How to require more than one thing from the command line


#1

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


#2

Roger P. 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


#3

Roger P. wrote:

Roger P. 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:inload’
from -e:1


#4

…and this does nothing:

$ ruby -e ‘x=10;’ r1test.rb
$


#5

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


#6

Nobuyoshi N. 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

  1. Isn’t specifying ‘binding’ redundant unless you acquire a binding
    from a different scope?

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

$ ruby -e ‘x=10; eval(File.read(ARGV[0]) )’ r1test.rb


#7

7stud – wrote:

…and this does nothing:

$ ruby -e ‘x=10;’ r1test.rb
$

  1. 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.


#8

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

  1. Isn’t specifying ‘binding’ redundant unless you acquire a binding
    from a different scope?

Just for explanation.

  1. 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.


#9

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 :slight_smile:
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