Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7996465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:42:57+00:00 2026-06-04T14:42:57+00:00

IRB is a nice way to play around and test things in Ruby. It’s

  • 0

IRB is a nice way to play around and test things in Ruby. It’s also possible to do some setup using a script, like this:

require 'irb'

class Pirate
  def greet
    puts "Arrrrr, nice to meet ya"
  end
end

IRB.start # You can now instantiate Pirate in IRB

One thing I’m unclear on, however, is variable scope when doing this. If I add these lines before IRB.start:

smithy     = Pirate.new
@blackbard = Pirate.new

… @blackbeard will be available in IRB, but referencing smithy will get undefined local variable or method 'smithy' for main:Object.

Why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:42:58+00:00Added an answer on June 4, 2026 at 2:42 pm

    The Binding that is used to evaluate the code is set up in irb/workspace.rb:51 (I’m referring to Ruby 1.9.3 rev 35410 here):

    @binding = eval("def irb_binding; binding; end; irb_binding",
                    TOPLEVEL_BINDING,
                    __FILE__,
                    __LINE__ - 3)
    

    That means that your IRB session runs in the same context as code inside a top-level method. Observe:

    puts "Outer object ID: %d" % self.object_id
    puts "Outer binding: " + binding.inspect
    smithy     = Pirate.new
    @blackbard = Pirate.new
    
    def test
      puts "Inner object ID: %d" % self.object_id
      puts "Inner binding: " + binding.inspect
      p @blackbard
      p smithy
    end
    test
    

    Output:

    Outer object ID: 13230960
    Outer binding: #<Binding:0x00000001c9aee0>
    Inner object ID: 13230960
    Inner binding: #<Binding:0x00000001c9acd8>
    #<Pirate:0x00000001c9ada0>
    /test.rb:18:in `test': undefined local variable or method `smithy' for main:Object (NameError)
        ...
    

    Note that the object context (self) is the same both inside and outside the function. This is because every top-level method is added to the global main object.

    Also note that the bindings inside and outside of the method differ. In Ruby, every method has its own name scope. This is why you can’t access the local name from inside IRB, while you can access the instance variable.

    To be honest, IRB is not the most glorious piece of Ruby software. I usually use Pry for this kind of stuff, using which you can just do:

    require 'pry'
    binding.pry
    

    And have a session with access to the current local variables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got the following error when using Ruby irb to test simple twitter status update
Several REPLs (like ruby's irb ) have some very useful features, such as using
Please look at the two examples below: irb(#<ActionView::Base:0x2863d58>):030:0> paintings_path => /some-nice-alias-path irb(#<ActionView::Base:0x2863d58>):029:0> self.controller_name.to_s +
I love using irb to quickly check how my ruby code reacts. Im wondering
Currently I am exploring the world of ruby. Playing around in the irb you
Can I drop to an IRB prompt from a running Ruby script? I want
Is it possible to run Ruby IRB ver a named pipe, so I can
A ruby array can be initialized using: irb(main):01:0> Array[1,2,3] => [1, 2, 3] How
In Ruby main, you can embed IRB inside of a Ruby script as described
I'm doing some design/debugging in IRB and need to login a user and then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.