Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IRB.start not starting

Tags:

ruby

gem

irb

I have some code that runs IRB.start (from inside a gem)

However, I get this error in magic-file.rb "No such file or directory @ rb_sysopen - console"

/Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb/magic-file.rb:7:in `initialize': No such file or directory @ rb_sysopen - console (Errno::ENOENT)
    from /Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb/magic-file.rb:7:in `open'
    from /Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb/magic-file.rb:7:in `open'
    from /Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb/input-method.rb:100:in `initialize'
    from /Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb/context.rb:84:in `new'
    from /Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb/context.rb:84:in `initialize'
    from /Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb.rb:426:in `new'
    from /Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb.rb:426:in `initialize'
    from /Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb.rb:381:in `new'
    from /Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb.rb:381:in `start'
    from /Users/you/projects/mygem/lib/mygem/commands.rb:20:in `console'
like image 486
Some Guy Avatar asked Sep 26 '22 12:09

Some Guy


1 Answers

I figured it out! By looking through the code:

  • https://github.com/ruby/ruby/blob/trunk/lib/irb.rb
  • https://github.com/ruby/ruby/tree/trunk/lib/irb/

I was able to determine that IRB checks ARGV so that you can pass IRB args on the commandline to whatever script you're invoking IRB.start in.

But since I was trying to create a rake task to start a console with my libraries preloaded for convenience, my command line was:

rake console

And IRB was trying to load the file 'console', because it assumes the first arg without a hyphen is a script to run. Kind of annoying. (IRB.start should take args as a method arg to avoid this tight coupling.)

I managed to fix it by adding this before my IRB.start:

ARGV.clear

UPDATE: Since it was requested, here's exactly where ARGV is used:

  • https://github.com/ruby/ruby/blob/trunk/lib/irb/init.rb#L124
like image 176
odigity Avatar answered Dec 31 '22 21:12

odigity