Project

General

Profile

Bug #15371

IRB with ARGV

Added by svnpenn (Steven Penny) 10 days ago. Updated 9 days ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:90239]

Description

PHP allows you to pass ARGV in interactive mode:

$ php -a -- alpha beta gamma
php > print $argv[3] . PHP_EOL;
gamma

and Python offers 4 ways:

$ python3 - alpha beta gamma
>>> import sys
>>> print(sys.argv[3])
gamma

$ python3 -- - alpha beta gamma
>>> import sys
>>> print(sys.argv[3])
gamma

$ python3 -i - alpha beta gamma
>>> import sys
>>> print(sys.argv[3])
gamma

$ python3 -i -- - alpha beta gamma
>>> import sys
>>> print(sys.argv[3])
gamma

However IRB seems to have no way to accomplish this:

$ irb - alpha beta gamma
/usr/share/ruby/2.3.0/irb/init.rb:213:in `parse_opts':
Unrecognized switch: - (IRB::UnrecognizedSwitch)

$ irb -- alpha beta gamma
/usr/share/ruby/2.3.0/irb/magic-file.rb:8:in `initialize': No such file or
directory @ rb_sysopen - alpha (Errno::ENOENT)

$ irb -- - alpha beta gamma
/usr/share/ruby/2.3.0/irb/magic-file.rb:8:in `initialize': No such file or
directory @ rb_sysopen - - (Errno::ENOENT)

$ irb - -- alpha beta gamma
/usr/share/ruby/2.3.0/irb/init.rb:213:in `parse_opts':
Unrecognized switch: - (IRB::UnrecognizedSwitch)

History

#1 [ruby-core:90267] Updated by shevegen (Robert A. Heiler) 10 days ago

I can not say whether the above is a bug or not; however had a grep
shows that there are two toplevel methods for IRB to make use
of ARGV, both in init.rb:

init.rb:  def IRB.setup(ap_path, argv: ::ARGV)
init.rb:  def IRB.parse_opts(argv: ::ARGV)

By the way, I find it slightly amusing that python's "there is only
one way" became "there are four ways". ;-)

#2 [ruby-core:90268] Updated by svnpenn (Steven Penny) 9 days ago

shevegen (Robert A. Heiler) wrote:

By the way, I find it slightly amusing that python's "there is only
one way" became "there are four ways". ;-)

Its actually only 2 ways if you want a true REPL:

$ python3 -i - alpha beta gamma
>>> import sys
>>> sys.argv[3]
'gamma'

$ python3 -i -- - alpha beta gamma
>>> import sys
>>> sys.argv[3]
'gamma'

Further, looks like PRY has the same issue as RUBY:

http://github.com/pry/pry/issues/1901

Workaround is to use RIPL:

$ ripl - alpha beta gamma
>> ARGV[3]
=> "gamma"

$ ripl -- alpha beta gamma
>> ARGV[3]
=> "gamma"

http://github.com/cldwalker/ripl

Also available in: Atom PDF