Too Cool for Internet Explorer

Sunday, January 27, 2008

Ruby and IRB

IRB is a Ruby tool that I’m not a fan. When a Ruby newbie uses it, there are some kind of behavior that most confuses then clarify.

But there are a lot of users, so I need to understand and explain how it works.

IRB, created by Keiju Ishitsuka, stands for Interactive Ruby Shell, and acts like expected, you send it a Ruby statement, that statement is executed and IRB return the last evaluated expression and some other tips.

But How ?

By nature, if used without arguments the Ruby interpreter wait for additional information.

D:\>ruby  

3.times do

puts 'step'

end

^D

step

step

step

D:\>

The Ruby interpreter, has two useful switches to achieve that:

  • -e let you specify a script on the command line to be executed.
  • -n wraps the specified script in a "while gets()" loop.

So, Keiju has used these to implement this tool. Try it yourself with the following command:

ruby -n -e 'puts eval($_)'

And IRB gives some useful tips on the command prompt. Just after started, IRB will show a command prompt like this:

irb(main):001:0>

This prompt has three parts delimited by “:” “irb(main)” are the actual program and session names, “001” is the actual line number and the final “0” is the indent level or in other words, the number of open blocks you currently have.

Indent level sample:

D:\>irb

irb(main):001:0> level = 0

=> 0

irb(main):002:0> if level == 0

irb(main):003:1> level = 1

irb(main):004:1> if level == 1

irb(main):005:2> level = 2

irb(main):006:2> end

irb(main):007:1> end

=> 2

irb(main):008:0> exit

D:\>

Program level and session name sample:

D:\>irb

irb(main):001:0> class Number

irb(main):002:1> def choice

irb(main):003:2> 'even'

irb(main):004:2> end

irb(main):005:1> end

=> nil

irb(main):006:0> mynumber = Number.new

=> #

irb(main):007:0> puts mynumber.choice

even

=> nil

irb(main):008:0> irb Number

irb#1(Number):001:0> def choice

irb#1(Number):002:1> 'odd'

irb#1(Number):003:1> end

=> nil

irb#1(Number):004:0> irb_exit

=> # irb::irb: context="# IRB::Context:0x2e46b58" , @signal_status=:IN_EVAL, @scanner=# rubylex:0x2e467fc

irb(main):009:0> puts mynumber.choice

odd

=> nil

irb(main):010:0> exit

D:\>

4 comments:

groupThink said...

Great post, I learned stuff I didn't know about irb.

Anonymous said...

Marcos,

I've seen the "teaser" back in FORPC101 and I had to visit your blog (already part of my RSS Feeds) to read about IRB.

Since the beginning that I like IRB, specially because it's like an interpreter and a "debugger" - all-in-one. It's very useful as a learning tool: I copy paste from a source and then paste it directly into IRB prompt. It then outputs what I need to better understand the code.

What I didn't know was that about line and indent level. Thanks so much for this useful information.

I'm also using wirble, both in Ubuntu and in Windows - with or without Cygwin. That's a helpful "code-beautifier", if I may say so.

Regards,
José Carlos Monteiro

Anonymous said...

Oi!

Aqui é o Matheus Zingarelli do curso de Ruby (FORPC101)!

Sei que seu blog está em inglês, mas me sinto mais à vontade falando em nossa língua mãe.

Seu post sobre IRB foi uma boa introdução. Eu gosto dos seus exemplos porque eles ensinam na prática o que muitas vezes eu encontro só na teoria.

Eu não entendi uma coisa... quando vc define o método choice após o comando 'irb Number' e sai dele, por que no irb(main) será esse método choice definido anteriormente o utilizado? Pensei que fosse o primeiro (que retornava even) que seria mostrado.

Tks, teacher =D

Marcos Ricardo said...

Hi Matheus,

The 'irb Number' command, establishes a section to the Number object (a class on this case), and from now on (until irb_exit), every command affects that object.

So, you should notice that we are redefining the choice method on the Number class, and we use the already defined instance (mynumber), to show that the method behavior has changed.

If the 'irb Number' command was not used, a new choice method will be defined at the main level, and the Number class choice method will not be affected.

Kind Regards.