Skip to content

binding.irb fails when used in BasicObject-derived class #1114

@trinistr

Description

@trinistr

Description

I expected that binding.irb would start an interactive session regardless of execution context's class. However, that is not the case, as IRB can't be found when called in BasicObject. Though, this behavior is inconsistent and depends both on Ruby version and irb version.

This script consistently reproduces the problem for me on Ruby 3.3.9, but not 3.2.9:

#!/usr/bin/env ruby
# frozen_string_literal: true

require "irb"

class A < Object
  def irb
    ::Kernel.binding.irb
  end
end

class B < BasicObject
  def irb
    ::Kernel.binding.irb
  end
end

class C < BasicObject
  define_method(:binding, ::Kernel.instance_method(:binding))

  def irb
    binding.irb
  end
end

# A.new.irb # works
# B.new.irb # uninitialized constant B::IRB (NameError)
C.new.irb # uninitialized constant C::IRB (NameError)
$ ruby -v
ruby 3.3.9 (2025-07-24 revision f5c772fc7c) [x86_64-linux]
$ ruby irb_test.rb 
irb_test.rb:22:in `irb': uninitialized constant C::IRB (NameError)

It seems to work on Ruby 3.2:

$ ruby -v         
ruby 3.2.9 (2025-07-24 revision 8f611e0c46) [x86_64-linux]
$ ruby irb_test.rb

From: irb_test.rb @ line 22 :

    17: 
    18: class C < BasicObject
    19:   define_method(:binding, ::Kernel.instance_method(:binding))
    20: 
    21:   def irb
 => 22:     binding.irb
    23:   end
    24: end
    25: 
    26: # A.new.irb # works
    27: # B.new.irb # uninitialized constant B::IRB (NameError)

Result of irb_info

irb(main):001> irb_info
Ruby version: 3.3.9
IRB version: irb 1.13.1 (2024-05-05)
InputMethod: RelineInputMethod with Reline 0.5.10
Completion: Autocomplete, RegexpCompletor
RUBY_PLATFORM: x86_64-linux
LANG env: en_US.utf-8
East Asian Ambiguous Width: 1

Terminal Emulator

What's your terminal emulator?

  • Doesn't matter, but happens in e.g. Tilda.

Setting Files

Are you using ~/.irbrc and ~/.inputrc?

  • No.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions