Learn Ruby in 20 Minutes: A Beginner’s Tutorial

This is a concise Ruby tutorial designed to be completed in approximately 20 minutes. It assumes you have already installed Ruby on your system. If you haven’t yet, please install Ruby before proceeding.

Diving into Interactive Ruby

Ruby includes a program that instantly displays the results of any Ruby code you input. This interactive environment is an excellent method for learning and experimenting with the Ruby language.

To begin, launch IRB (Interactive Ruby).

  • On macOS, open Terminal and type irb, then press Enter.
  • On Linux, open a shell and type irb and press Enter.
  • On Windows, find Interactive Ruby in the Ruby section of your Start Menu and open it.
<span>irb(main):001:0></span>

Once IRB is running, you’ll see the prompt irb(main):001:0>. Now, let’s try your first Ruby command.

Type: "Hello World"

<span>irb(main):001:0></span><span>"Hello World"</span> <span>=></span> <span>"Hello World"</span>

Witness Ruby in Action

What just happened here? Did we just write the simplest “Hello World” program? Not quite. The second line, => "Hello World", is IRB’s way of showing you the output of the last Ruby expression it evaluated. To actually print “Hello World” to the console, we need to use the puts command:

<span>irb(main):002:0></span><span>puts</span> <span>"Hello World"</span> <span>Hello World </span><span>=></span> <span>nil</span>

puts is a fundamental Ruby command used to display output. But what about => nil? This is the return value of the puts method. In Ruby, nil represents the absence of a value, and puts always returns nil after printing.

IRB as Your Calculator

One of the immediate benefits of IRB is using it as a calculator. Let’s try some basic arithmetic:

<span>irb(main):003:0></span><span>3</span><span>+</span><span>2</span> <span>=></span> <span>5</span>

Simple addition. Now, let’s multiply three by two. You could retype the whole line, but IRB allows you to easily edit previous commands. Try pressing the up-arrow key on your keyboard. This should bring back the line 3+2. Use the left arrow key to move the cursor just after the + sign, then use backspace to delete the + and type * for multiplication.

<span>irb(main):004:0></span><span>3</span><span>*</span><span>2</span> <span>=></span> <span>6</span>

Next, let’s calculate three squared:

<span>irb(main):005:0></span><span>3</span><span>**</span><span>2</span> <span>=></span> <span>9</span>

In Ruby, ** is the exponentiation operator, meaning “to the power of”. But what if you need to find the square root?

<span>irb(main):006:0></span><span>Math</span><span>.</span><span>sqrt</span><span>(</span><span>9</span><span>)</span> <span>=></span> <span>3.0</span>

Wait, what was Math.sqrt(9)? If you guessed it calculates the square root of nine, you’re correct. Let’s break it down: Math is a built-in Ruby module for mathematical functions.

Organizing Code with Modules

Modules in Ruby serve to group related methods together under a common name, like Math. Math includes various mathematical methods such as sin(), cos(), tan(), and more.

The dot (.) is used to call a method on a module or object. In this case, we are calling the sqrt() method (short for square root) from the Math module, and we are passing 9 as an argument to the method.

The result 3.0 is a floating-point number, not just 3. This is because square roots often result in non-integer values, so Math.sqrt() always returns a float to handle both integer and non-integer results.

Let’s store some of these calculation results in variables for later use:

<span>irb(main):007:0></span><span>a</span> <span>=</span> <span>3</span> <span>**</span> <span>2</span> <span>=></span> <span>9</span> <span>irb(main):008:0></span><span>b</span> <span>=</span> <span>4</span> <span>**</span> <span>2</span> <span>=></span> <span>16</span> <span>irb(main):009:0></span><span>Math</span><span>.</span><span>sqrt</span><span>(</span><span>a</span><span>+</span><span>b</span><span>)</span> <span>=></span> <span>5.0</span>

While IRB is a powerful calculator, let’s get back to more fundamental programming concepts as we continue to Learn Ruby. Let’s explore strings and more basic operations in the next steps of your Ruby journey.

Continue learning Ruby with more tutorials and guides.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *