Forum: Ruby how to require more than one thing from the command line

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2009-01-24 21:41
Is there a way to do this?
$ ruby -rrubygems -rfacets -e '3'
ruby: no such file to load -- facets (LoadError)

Also is there a way to do something like
$ ruby -e 'initialization code' filename.rb

?

Thanks!
-=r
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2009-02-14 06:19
Roger Pack wrote:
> Is there a way to do this?
> $ ruby -rrubygems -rfacets -e '3'
> ruby: no such file to load -- facets (LoadError)

Seems this is a gems quirk of some type.


> Also is there a way to do something like
> $ ruby -e 'initialization code' filename.rb

As per Nobu's advice.
ruby -e 'some pre init stuff; load($0 = ARGV.shift)' filename.rb

Something like that.
Thanks!
-=r
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-02-14 07:11
Roger Pack wrote:
> Roger Pack wrote:
>> Is there a way to do this?
>> $ ruby -rrubygems -rfacets -e '3'
>> ruby: no such file to load -- facets (LoadError)
>
> Seems this is a gems quirk of some type.
>
>
>> Also is there a way to do something like
>> $ ruby -e 'initialization code' filename.rb
>
> As per Nobu's advice.
> ruby -e 'some pre init stuff; load($0 = ARGV.shift)' filename.rb
>

That doesn't work for me:

r1test.rb
--------
puts "hello"
puts x


$ ruby -e 'x=10; load($0 = ARGV.shift)' r1test.rb
hello
./r1test.rb:2: undefined local variable or method `x' for main:Object
(NameError)
        from -e:1:in `load'
        from -e:1
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-02-14 07:13
...and this does nothing:

$ ruby -e 'x=10;' r1test.rb
$
F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 Nobuyoshi Nakada (nobu)
on 2009-02-14 07:22
(Received via mailing list)
Hi,

At Sat, 14 Feb 2009 15:09:46 +0900,
7stud -- wrote in [ruby-talk:328177]:
> That doesn't work for me:
>
> r1test.rb
> --------
> puts "hello"
> puts x

require separates the scope, so local variables defined outside
are not accessible.

$ ruby -e 'x=10; eval(File.read($0 = ARGV.shift), binding)' r1test.rb
hello
10
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-02-14 08:53
Nobuyoshi Nakada wrote:
> Hi,
>
> At Sat, 14 Feb 2009 15:09:46 +0900,
> 7stud -- wrote in [ruby-talk:328177]:
>> That doesn't work for me:
>>
>> r1test.rb
>> --------
>> puts "hello"
>> puts x
>
> require separates the scope, so local variables defined outside
> are not accessible.
>

1) require?  Where, what, when?

> $ ruby -e 'x=10; eval(File.read($0 = ARGV.shift), binding)' r1test.rb
> hello
> 10

2) Isn't specifying 'binding' redundant unless you acquire a binding
from a different scope?

3) Why the $0 = ARGV.shift ?  This appears to work the same way:

$ ruby -e 'x=10; eval(File.read(ARGV[0]) )' r1test.rb
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-02-14 09:06
7stud -- wrote:
> ...and this does nothing:
>
> $ ruby -e 'x=10;' r1test.rb
> $

4) Why does that do nothing?  Here is another example:

r1test.rb
-------
puts "world"

$ ruby -e 'puts "hello"' r1test.rb
hello
$

I expected the output to be:

$ ruby -e 'puts "hello"' r1test.rb
hello
world
$

According to pickaxe2, p 177-178:

------------------
A ruby command line consists of three parts: options to the Ruby
interpreter, optionally the name of the program to run, and optionally a
set of arguments for that program.

ruby [options] [--] [programfile] [arguments]

Command-Line Options

-e 'command'

If programfile is omitted when -e is present, execution stops after the
-e commands have been run.
--------------------

To me that implies that if programfile is present, then programfile will
execute after the -e commands.
F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 Nobuyoshi Nakada (nobu)
on 2009-02-14 10:51
(Received via mailing list)
Hi,

At Sat, 14 Feb 2009 16:52:12 +0900,
7stud -- wrote in [ruby-talk:328185]:
> > require separates the scope, so local variables defined outside
> > are not accessible.
> >
>
> 1) require?  Where, what, when?

Sorry, it was load.

> > $ ruby -e 'x=10; eval(File.read($0 = ARGV.shift), binding)' r1test.rb
> > hello
> > 10
>
> 2) Isn't specifying 'binding' redundant unless you acquire a binding
> from a different scope?

Just for explanation.

> 3) Why the $0 = ARGV.shift ?  This appears to work the same way:
>
> $ ruby -e 'x=10; eval(File.read(ARGV[0]) )' r1test.rb

For the idiom, if $0 == __FILE__.
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2009-02-14 19:10
> If programfile is omitted when -e is present, execution stops after the
> -e commands have been run.
> --------------------
>
> To me that implies that if programfile is present, then programfile will
> execute after the -e commands.

Yeah pickaxe is wrong there, I'm thinking :)
The good news is that it's still possible [as seen in previous posts in
the thread].  I suppose if you wanted to you could create a "wrapper"
for ruby that smartly understood what one meant with
-e 'stuff' programfile.rb

I was just curious if it was possible, having wanted to do it a few
times.
Thanks all.
-=r
This topic is locked and can not be replied to.