• 20 hours
  • Easy

Free online content available in this course.



Got it!

Last updated on 2/6/20

Write your first Ruby code

Log in or subscribe for free to enjoy all this course has to offer!

We'll use a Ruby tool called irb, which stands for interactive Ruby, in order to write Ruby code.

What does irb do?

Irb allows you to type Ruby code that will immediately execute and spit back results. This sort of instant feedback will help you progress as a beginner. You mustn't be scared to type in all sorts of operations into irb and see what the result is!

Following along in this course

We'll start by running some pre-written Ruby snippets so you can see irb execute quickly. This instant execution is very gratifying as a beginner! Afterwards, we'll start breaking down the Ruby code itself.

You have two options for this course:

  1. Work using applications on your computer using either Terminal, Sublime Text, or both. Nonetheless, this is the more complicated option.

  2. Work in the interactive code editor shown in each chapter.

Interactive, online code editor (easier)

I've added interactive code environments to this course that will allow you to follow along without any work or files on your computer! 

The first one appears later in this chapter. This saves you the hassle of installing Ruby on your computer for now.

Terminal on your computer (harder)

If you want to follow along on your computer (not in your browser, like Chrome or Safari), that's great! 

To launch irb (the tool that allows Ruby commands to run instantly and safely), launch Terminal. The location of Terminal will depend on your operating system (Mac, Windows, etc). This course doesn't cover command line technicalities (though we're soon releasing a course on the exact subject)! 

First Ruby code

Let's roll! Here's the interactive code exercise for this chapter:


The first thing you need to do either in the online code editor or in Terminal on your computer is to type the following command and hit Enter.


You should see this line in your console upon launching irb:


Let's first take a snippet of Ruby code we saw in the first course chapter and run it through irb. Simply copy and paste this line of code into your command line (either online or in Terminal), and hit Enter.

5.times do print "Ruby is great! " end

Once you've run the command, your console will now look like this:

irb(main):001:0> 5.times do print "Ruby is great! " end
Ruby is great! Ruby is great! Ruby is great! Ruby is great! Ruby is great! => 5

 As expected, we have the sentence"Ruby is great! "printed 5 times in our console.

Now, try changing the number 5to something else or the sentence"Ruby is great! "to another sentence or word. Don't be afraid to try new things!

Exiting irb

The last important point is to cover how to get out of irb! In order to live this interactive environment, simply typeexitand hit Enter

Example of certificate of achievement
Example of certificate of achievement