how to require more than one thing from the command line

R

Roger Pack

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
 
R

Roger Pack

Roger said:
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
 
7

7stud --

Roger said:
Seems this is a gems quirk of some type.



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
 
N

Nobuyoshi Nakada

Hi,

At Sat, 14 Feb 2009 15:09:46 +0900,
7stud -- wrote in [ruby-talk:328177]:
That doesn't work for me:

r1test.rb

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
 
7

7stud --

Nobuyoshi said:
Hi,

At Sat, 14 Feb 2009 15:09:46 +0900,
7stud -- wrote in [ruby-talk:328177]:
That doesn't work for me:

r1test.rb

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
 
7

7stud --

7stud said:
...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.
 
N

Nobuyoshi Nakada

Hi,

At Sat, 14 Feb 2009 16:52:12 +0900,
7stud -- wrote in [ruby-talk:328185]:
1) require? Where, what, when?

Sorry, it was load.
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__.
 
R

Roger Pack

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top