Learn to Code via Tutorials on Repl.it!

← Back to all posts
Basic Ruby Tutorial
JWZ6 (243)


Before we start, I want to give you a little background information on the creator of Ruby, Yukihiro Matsumoto.

Yukihiro is a pro programmer. He says that his favorite languages are Perl, Smalltalk, Eiffel, Ada, and Lisp. He tried to combine them, making Ruby. Yukihiro tried to make Ruby natural, mirroring with life. Ruby is a language that people absolutely love.

Now we can get started.

2. Text
3. Lists
4. If, Elsif, Else
5. Colorize Text
6. Time Loop
7. While Loop
8. Infinite Loop
9. Math
10. Clear Screen And Sleep Function
11. Ending

So welcome to my ruby basic tutorial. This covers hard easy and basic ruby. So let's first start of with comments! This is a ONE-line comment:

# This is a a one-line comment. It's a lot like a one-line python comment

That right up there is a very simple comment. For a MULTI-line comment, we have:

This is a multi-line comment. It looks cool, I guess.

Now for printing text! If we want to make sure that our text automatically shifts to a new line once finished, do:

puts “I Am"
puts "Mr Yeet"

The output looks like this:

I Am
Mr Yeet

If you want your text to keep on the same line, do:

print "I Am"
print "Mr Yeet"

The output will look like this:

I Am Mr Yeet

P is another way to show output. It looks like this:

p "I Am Mr Yeet"


"I Am Mr Yeet"


The lists in Python are not very similar to the lists in Ruby. They look like this:

list_example=["hi", "hey", "nice", "cool", "epic"]

Using the list looks like this:

puts list_example
p list_example


["hi", "hey", "nice", "cool", "epic"]

Note: If you want to use boolean, or string, or float, Ruby already converts that for you so use them however you want.

If, Elsif, Else

How would you get user input? You do:

puts "What is 1 + 1?“
two = gets.chomp

Using if, elsif, and else* looks like this:

if "#{two}" == '2'
  puts "correct"
elsif "#{two}" == '4'
  puts "that's multiplying, not adding"
  puts "incorrect"

Make sure that you have end at the end.

Coloring Text

There are two ways to colorize text. The first way is to use colorize. It looks like this:

require "colorize"

puts "hi".yellow
puts "yo".red
puts "lol".green

You get the idea, right?

If you want to be lazy, just copy paste this:

class String
def black;          "\e[30m#{self}\e[0m" end
def red;            "\e[31m#{self}\e[0m" end
def green;          "\e[32m#{self}\e[0m" end
def brown;          "\e[33m#{self}\e[0m" end
def blue;           "\e[34m#{self}\e[0m" end
def magenta;        "\e[35m#{self}\e[0m" end
def cyan;           "\e[36m#{self}\e[0m" end
def gray;           "\e[37m#{self}\e[0m" end

def bg_black;       "\e[40m#{self}\e[0m" end
def bg_red;         "\e[41m#{self}\e[0m" end
def bg_green;       "\e[42m#{self}\e[0m" end
def bg_brown;       "\e[43m#{self}\e[0m" end
def bg_blue;        "\e[44m#{self}\e[0m" end
def bg_magenta;     "\e[45m#{self}\e[0m" end
def bg_cyan;        "\e[46m#{self}\e[0m" end
def bg_gray;        "\e[47m#{self}\e[0m" end

def bold;           "\e[1m#{self}\e[22m" end
def italic;         "\e[3m#{self}\e[23m" end
def underline;      "\e[4m#{self}\e[24m" end
def blink;          "\e[5m#{self}\e[25m" end
def reverse_color;  "\e[7m#{self}\e[27m" end

Those are the color codes above. You can use them like this:

puts "hi".cyan
puts "noice".magenta
puts "snek attak".bg_green.red

Time Loops

If you want something to repeat more than once, use this loop:

5.times do
puts "This will be printed only 5 times"

While Loops

Let's say that you want to have a while loop, similar to a Python while loop. How about we use some variables:

$king = 60
$number = -1

For the actual while loop, we have:

$king = 60
$number = -1
   puts("Look: = #$king" )
   $king -=1
end while $king > $number

That up there prints every number greater than -1 and less than 61. It's considered a while loop. Make sure not to end with:

end while  $king = $number

That would be a disaster.

Infinite Loops

If you do something like this:...

loop do
  puts "yeet"

you would have an infinite loop. Just do

loop do
  #whatever you want


How do you do math in Ruby? It's a lot like Python. For example, if I do:

x = 60
y = 4
puts x/y

I would get 15!If I wanted to do adding, sutracting, and multiplying, I would have:

x = 60
y = 4
$q = x/y
$p = x*y
$c = x+y
$b = x-y
puts "X divided by Y = #$q" 
puts "X subtract Y = #$b"
puts "X plus Y = #$c"
puts "X times Y = #$p

That's how you do math.

Clear Screen and Sleep Function

How to clear screen:

system('cls') #for windows

How to use sleep function:

sleep(#how many seconds you want)

That's it! I hope you enjoyed this! XD