Learning Ruby series 1 of 5

Nagaraj Bettadapura
3 min readFeb 28, 2021

--

Nothing beats learning a programming language by doing it, and doing it in small increments. Ruby provides tools to do just that. You can learn, practice and experiment by using the built-in irb tool. irb is an interactive ruby interpreter that gives you immediate feedback helpful hints and results.

With that brief introduction, let us dive in. Oh, and. we are assuming you have ruby installed already

On your command prompt or terminal, type irb. You should see the prompt change to:

irb(main):001:0>

It shows the irb prompt activated, along with the line number (001) and the number after “:” displays the nesting within. Type exit @ the prompt to go exit irb and back to your native os terminal or prompt. You could also do a Ctrl+C or Ctrl+Z to exit the irb.

If you prefer a cleaner irb prompt without the number lines and nested numbers, then type irb — simple-prompt. You will see this:

>>

You can still do your interactive ruby learning without the added line and nest details. Now let us begin the obligatory initial show and tell. You can type the commands and see the results for yourself:

>> 10
=> 10
>> "hello"
=> "hello"
>> 10 + 10 #irb is your handy calculator
=> 20
>> (10+20)/2 #math expression with its results in the next line
=> 15 #prints the value of the mathematical expression
"> x = "Hello" #assigning the value "Hello" to a variable x
=> "Hello" #prints the value of the expression above
>> y = 25
=> 25

Feel free to continue experimenting with math expressions, variable assignments, etc.. This is just the beginning. We will get into the more fun stuff in the next chapter. Before we close out this lesson, a quick summary of irb and ruby:

$ ruby --version
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]
# $ is a global variable output below of installed ruby versions
$ ruby -e 'puts $:'
/home/username/.rbenv/rbenv.d/exec/gem-rehash
/home/username/.rbenv/versions/3.0.0/lib/ruby/site_ruby/3.0.0
/home/username/.rbenv/versions/3.0.0/lib/ruby/site_ruby/3.0.0/x86_64-linux
/home/username/.rbenv/versions/3.0.0/lib/ruby/site_ruby
/home/username/.rbenv/versions/3.0.0/lib/ruby/vendor_ruby/3.0.0
/home/username/.rbenv/versions/3.0.0/lib/ruby/vendor_ruby/3.0.0/x86_64-linux
/home/username/.rbenv/versions/3.0.0/lib/ruby/vendor_ruby
/home/username/.rbenv/versions/3.0.0/lib/ruby/3.0.0
/home/username/.rbenv/versions/3.0.0/lib/ruby/3.0.0/x86_64-linux
>> RbConfig::CONFIG["bindir"] # where is ruby installed
=> "/home/username/.rbenv/versions/3.0.0/bin"

Variables in Ruby:

Local variables ex: name = “John”. Here name is your local variable, and is assigned a value of “John”

Instance variable ex: @name = “John”. Here @ symbol makes the variable name an Instance variable. We will learn more about this in the upcoming chapters.

Class variable ex: @@name = “John”. Here @@ symbol makes the variable name a Class variable.

Global variable ex: $name = “John”. Here, $ symbol makes the variable name a Global variable.

The primary difference between the different variable types is the scope and ability to access the value of those variables from different areas of your ruby program.

In addition to variables, ruby also has CONSTANTS, Keywords and Methods like other programming languages. Unlike a variable, constants like the name suggests has its value static, and is not expected to change.

Summary

We have had a whirlwind tour of irb and its basics, types of variables, basic assignments and expressions. We will next move on to learning about ruby objects, method definitions, communicating with objects. It is going to get a little more technical but still a lot of FUN

Read about Ruby Objects and Methods

--

--

No responses yet