ruby/irb

IRB crashes when using `define_method('class') {}`

koic opened this issue · 1 comments

koic commented

Description

I was expecting no errors. However, the following error occurs:

$ irb
irb(main):001> define_method('class') { puts 'hi' }
=> :class
hi
hi
hi
/Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb/history.rb:38:in `save_history': nil is not a class/module (TypeError)

      history = self.class::HISTORY.to_a
                          ^^^^^^^^^
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1031:in `ensure in run'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1032:in `run'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:904:in `start'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/exe/irb:9:in `<top (required)>'
        from /Users/koic/.rbenv/versions/3.3.1/bin/irb:25:in `load'
        from /Users/koic/.rbenv/versions/3.3.1/bin/irb:25:in `<main>'
/Users/koic/.rbenv/versions/3.3.1/lib/ruby/3.3.0/ripper/lexer.rb:80:in `&': undefined method `new' for nil (NoMethodError)

      def &(i) self.class.new(to_int & i) end
                         ^^^^
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb/color.rb:220:in `dispatch_seq'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb/color.rb:151:in `block (2 levels) in colorize_code'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb/color.rb:149:in `each_line'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb/color.rb:149:in `block in colorize_code'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb/color.rb:197:in `block (2 levels) in scan'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb/color.rb:204:in `block (2 levels) in scan'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb/color.rb:202:in `each'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb/color.rb:202:in `block in scan'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb/ruby-lex.rb:47:in `compile_with_errors_suppressed'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb/color.rb:179:in `scan'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb/color.rb:136:in `colorize_code'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb/input-method.rb:271:in `block in initialize'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/reline-0.5.7/lib/reline/line_editor.rb:798:in `modify_lines'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/reline-0.5.7/lib/reline/line_editor.rb:351:in `block in modified_lines'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/reline-0.5.7/lib/reline/line_editor.rb:344:in `with_cache'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/reline-0.5.7/lib/reline/line_editor.rb:350:in `modified_lines'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/reline-0.5.7/lib/reline/line_editor.rb:374:in `wrapped_prompt_and_input_lines'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/reline-0.5.7/lib/reline/line_editor.rb:439:in `wrapped_cursor_position'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/reline-0.5.7/lib/reline/line_editor.rb:452:in `update_dialogs'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/reline-0.5.7/lib/reline.rb:346:in `inner_readline'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/reline-0.5.7/lib/reline.rb:268:in `block (2 levels) in readmultiline'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/reline-0.5.7/lib/reline/ansi.rb:162:in `block in with_raw_input'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/reline-0.5.7/lib/reline/ansi.rb:162:in `raw'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/reline-0.5.7/lib/reline/ansi.rb:162:in `with_raw_input'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/reline-0.5.7/lib/reline.rb:267:in `block in readmultiline'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/reline-0.5.7/lib/reline.rb:261:in `synchronize'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/reline-0.5.7/lib/reline.rb:261:in `readmultiline'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/3.3.0/forwardable.rb:240:in `readmultiline'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb/input-method.rb:474:in `gets'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1073:in `block in read_input'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1380:in `signal_status'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1071:in `read_input'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1093:in `readmultiline'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1118:in `block in each_top_level_statement'
        from <internal:kernel>:187:in `loop'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1117:in `each_top_level_statement'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1040:in `eval_input'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1021:in `block in run'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1020:in `catch'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1020:in `run'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:904:in `start'
        from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.13.1/exe/irb:9:in `<top (required)>'
        from /Users/koic/.rbenv/versions/3.3.1/bin/irb:25:in `load'
        from /Users/koic/.rbenv/versions/3.3.1/bin/irb:25:in `<main>'

I'm not sure if this is due to an implementation constraint, anyway I'm reporting it.

Result of irb_info

Please paste the result of irb_info command in IRB.

ex.)

$ irb
irb(main):001> irb_info
Ruby version: 3.3.1
IRB version: irb 1.13.1 (2024-05-05)
InputMethod: RelineInputMethod with Reline 0.5.7 and /Users/koic/.inputrc
Completion: Autocomplete, RegexpCompletor
.irbrc paths: /Users/koic/.irbrc
RUBY_PLATFORM: x86_64-darwin23
East Asian Ambiguous Width: 1
=> nil

Terminal Emulator

iTerm2 Build 3.0.15

Setting Files

$ cat ~/.irbrc
IRB.conf[:AUTO_INDENT] = true
IRB.conf[:USE_READLINE] = false # Use Reline instead of Readline.
IRB.conf[:SAVE_HISTORY] = 2000
IRB.conf[:HISTORY_PATH] = File.expand_path('~/.irb-history')

Thank you for your report. This overrides Object#class. Since Object#class is used in IRB, it causes a crash. Please define the method with a different name :)