Ruby Programming: Interactive shell

Once we installed Ruby, we will get one powerful command line interactive Ruby-programming environment called irb (Interactive Ruby)irb is a command line interpreter allowed to enter Ruby commands and instantly see the results in the environment itself. This is very useful when learning Ruby.

In this article we will discuss about invoking ruby command-line interpreter, and using commands at irb shell.

Step 1. To invoke Ruby’s interactive shell; type the below command at command prompt.

irb

Step 2. Once interactive shell is opened and it will display a shell prompt where we can enter our Ruby commands. The shell prompt looks like below:

irb(main):001:0>

Step 3. From interactive shell, we can type Ruby commands and we will get immediate responses from the shell. For example: see the below command to display the addition of two numbers 2 and 3.

irb(main):001:0> 2+3
=> 5

Step 4. We can enter Ruby methods at shell prompt. For example: below command will display “Hello, World!” message.

irb(main):002:0> puts "Hello, World!"
Hello, World!
=> nil

But why “nil”? Because puts is a method, it will display the text which is passed through an argument and it doesn’t return anything; hence “nil”.

Step 5. We can declare variables also at shell prompt. For example: see the below lines, these will declare “a” and “b” variables and display the result of the command “a*b” at the shell prompt.

irb(main):003:0> a=20
=> 20
irb(main):004:0> b=3
=> 3
irb(main):005:0> a*b
=> 60

Step 6. We can write functions at shell prompt. For example: below commands define the function “DisplayMessage” and called the function from shell prompt. “DisplayMessage” method simply prints the message whatever we passed through its argument at the shell prompt.

irb(main):006:0> def DisplayMessage(message)
irb(main):007:1> puts message
irb(main):008:1> end
=> nil
irb(main):009:0> DisplayMessage("Hello, Ruby!")
Hello, Ruby!
=> nil

Step 7. Even we can define Ruby classes at shell prompt. Below example explains the same:

irb(main):001:0> class SampleClass
irb(main):002:1> def SayHello
irb(main):003:2> puts "Hello!"
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> o = SampleClass.new
=> #<SampleClass:0x0000000312cb38>
irb(main):007:0> o.SayHello
Hello!
=> nil
irb(main):008:0>

Above command created a Ruby class “SampleClass” with a method “SayHello”. Once the class is created, created the instance of the class “o”. From class object, called its method “SayHello”. Finally, it displayed the result “Hello!” at the shell prompt.

Step 8. We can even open sub-irbs from the shell prompt; just type the irb at shell prompt.

irb(main):001:0> irb
irb#1(main):001:0>

Step 9. Once we are done with our experiments in irb; we need to quit from irb. Just type “exit” or “quit” at shell prompt to quit from irb or sub-irb. If it is a sub-irb, it quits from sub-irb and enters into main irb session.

irb#1(main):001:0> quit
=> #<IRB::Irb: @context=#<IRB::Context:0x00000002bdc868>, @signal_status=:IN_EVAL, scanner=#<RubyLex:0x00000002c87588>>
irb(main):002:0> quit

irb is the best shell to practice Ruby commands or expressions.

🙂 Sahida

Leave a Reply