Skip to Content
Ruby Pocket Reference
book

Ruby Pocket Reference

by Michael Fitzgerald
July 2007
Beginner to intermediate
176 pages
2h 30m
English
O'Reilly Media, Inc.
Content preview from Ruby Pocket Reference

Interactive Ruby (irb)

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> exit

You 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> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Ruby Pocket Reference, 2nd Edition

Ruby Pocket Reference, 2nd Edition

Michael Fitzgerald
Ruby Phrasebook

Ruby Phrasebook

Jason Clinton
Ruby by Example

Ruby by Example

Kevin C. Baird

Publisher Resources

ISBN: 9780596514815Errata Page