This is a small Ruby tutorial that should take no more than 20 minutes to complete. It assumes you have Ruby installed already. (If you don't have Ruby on your computer, download and install it before you start.)

Interactive Ruby

Ruby comes with a program that will show the results of any Ruby statements you feed it. Playing with Ruby code in interactive sessions like this is a terrific way to learn the language. Open up IRB (which stands for Interactive Ruby).

If you're using macOS open up a Terminal and type irb. If you're on Linux, open up a shell and type irb. If you're using Windows, open Interactive Ruby from the Ruby section of your Start Menu.

irb(main):001:0>

Ok, it's open. Now what?

Type this: puts "Hello World"

irb(main):001:0> puts "Hello World"
Hello World
=> nil

Your Free Calculator

IRB is already useful as a calculator:

irb(main):002:0> 3+2
=> 5
irb(main):003:0> 3*2
=> 6
irb(main):004:0> Math.sqrt(9)
=> 3.0

Defining a Method

irb(main):010:0> def hi
irb(main):011:1>   puts "Hello World!"
irb(main):012:1> end
=> nil

The code def hi starts the definition of a method. It tells Ruby that we're defining a method, that its name is hi. The next line is the body of the method, the same line we saw earlier: puts "Hello World". Finally, the last line end tells Ruby we're done defining the method. Let's try running this method a few times:

irb(main):013:0> hi
Hello World!
=> nil
irb(main):014:0> hi()
Hello World!
=> nil

Continue reading: Part 2 | Part 3 | Part 4