Class: Binding (Ruby 2.7.5)
Description: Class : Binding - Ruby 2.7.5

### Home Classes Methods

### In Files

- irb.rb

### Parent

Object

### Methods

- #irb

### Class/Module Index

Quicksearch

- Binding
- IRB::Abort
- IRB::Canvas
- IRB::Color
- IRB::Color::SymbolState
- IRB::Context
- IRB::ContextExtender
- IRB::ExtendCommandBundle
- IRB::FileInputMethod
- IRB::Frame
- IRB::Frame::FrameOverflow
- IRB::Frame::FrameUnderflow
- IRB::History
- IRB::InputMethod
- IRB::Inspector
- IRB::Irb
- IRB::IrbLoader
- IRB::JobManager
- IRB::LoadAbort
- IRB::MethodExtender
- IRB::Notifier
- IRB::Notifier::AbstractNotifier
- IRB::Notifier::CompositeNotifier
- IRB::Notifier::ErrUndefinedNotifier
- IRB::Notifier::ErrUnrecognizedLevel
- IRB::Notifier::LeveledNotifier
- IRB::Notifier::NoMsgNotifier
- IRB::OutputMethod
- IRB::OutputMethod::NotImplementedError
- IRB::ReadlineInputMethod
- IRB::ReidlineInputMethod
- IRB::RubyModel
- IRB::StdioInputMethod
- IRB::StdioOutputMethod
- IRB::Vec
- IRB::WorkSpace
- Object
- XMP
- XMP::StringInputMethod

No matching classes.

# Binding

### Public Instance Methods

irb() click to toggle source

Opens an IRB session where `binding.irb` is called which allows for interactive debugging. You can call any methods or variables available in the current scope, and mutate state if you need to.

Given a Ruby file called `potato.rb` containing the following code:

    class Potato
      def initialize
        @cooked = false
        binding.irb
        puts "Cooked potato: #{@cooked}"
      end
    end

    Potato.new

Running `ruby potato.rb` will open an IRB session where `binding.irb` is called, and you will see the following:

    $ ruby potato.rb

    From: potato.rb @ line 4 :

        1: class Potato
        2:   def initialize
        3:     @cooked = false
     => 4:     binding.irb
        5:     puts "Cooked potato: #{@cooked}"
        6:   end
        7: end
        8:
        9: Potato.new

    irb(#<Potato:0x00007feea1916670>):001:0>

You can type any valid Ruby code and it will be evaluated in the current context. This allows you to debug without having to run your code repeatedly:

    irb(#<Potato:0x00007feea1916670>):001:0> @cooked
    => false
    irb(#<Potato:0x00007feea1916670>):002:0> self.class
    => Potato
    irb(#<Potato:0x00007feea1916670>):003:0> caller.first
    => ".../2.5.1/lib/ruby/2.5.0/irb/workspace.rb:85:in `eval'"
    irb(#<Potato:0x00007feea1916670>):004:0> @cooked = true
    => true

You can exit the IRB session with the `exit` command. Note that exiting will resume execution where `binding.irb` had paused it, as you can see from the output printed to standard output in this example:

    irb(#<Potato:0x00007feea1916670>):005:0> exit
    Cooked potato: true

See IRB for more information.

                   # File irb.rb, line 901
    def irb
      IRB.setup(source_location[0], argv: [])
      workspace = IRB::WorkSpace.new(self)
      STDOUT.print(workspace.code_around_binding)
      binding_irb = IRB::Irb.new(workspace)
      binding_irb.context.irb_path = File.expand_path(source_location[0])
      binding_irb.run(IRB.conf)
    end