July 2007
Beginner to intermediate
176 pages
2h 30m
English
Interactive Ruby or irb is an interactive programming environment that comes with Ruby. It was written by Keiju Ishitsuka. To invoke it, type irb at a shell or command prompt, and begin entering Ruby statements and expressions. Use exit or quit to exit irb. Here is a sample of irb evaluating a variety of expressions:
$ irb
irb(main):001:0> 23 + 27
=> 50
irb(main):002:0> 50 - 23
=> 27
irb(main):003:0> 10 * 5
=> 50
irb(main):004:0> 10**5
=> 100000
irb(main):005:0> 50 / 5
=> 10
irb(main):006:0> x = 1
=> 1
irb(main):007:0> x + 59
=> 60
irb(main):008:0> hi = "Hello, Matz!"
=> "Hello, Matz!"
irb(main):009:0> hi.each { |s| print s }
Hello, Matz!=> "Hello, Matz!"
irb(main):010:0> 1.upto( 10 ) { |n| print n, " " }
1 2 3 4 5 6 7 8 9 10 => 1
irb(main):011:0> 100 < 1_000
=> true
irb(main):012:0> class Hello
irb(main):013:1> attr :hi, true
irb(main):014:1> end
=> nil
irb(main):015:0> h = Hello.new
=> #<Hello:0x3602cc>
irb(main):016:0> h.hi="Hello, Matz!"
=> "Hello, Matz!"
irb(main):017:0> h.hi
=> "Hello, Matz!"
irb(main):018:0> self
=> main
irb(main):019:0> self.class
=> Object
irb(main):020:0> exitYou can also invoke a single program with irb. After running the program, irb exits:
$ cat hello.rb #!/usr/bin/env ruby class Hello def initialize( hello ) @hello = hello end def hello @hello end end salute = Hello.new( "Hello, Matz!" ) puts salute.hello $ irb hello.rb hello.rb(main):001:0> #!/usr/bin/env ruby hello.rb(main):002:0* hello.rb(main):003:0* class Hello hello.rb(main):004:1> ...
Read now
Unlock full access