[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to require more than one thing from the command line

Roger Pack

1/24/2009 8:40:00 PM

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
--
Posted via http://www.ruby-....

8 Answers

Roger Pack

2/14/2009 5:18:00 AM

0

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
--
Posted via http://www.ruby-....

7stud --

2/14/2009 6:10:00 AM

0

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


--
Posted via http://www.ruby-....

7stud --

2/14/2009 6:12:00 AM

0

...and this does nothing:

$ ruby -e 'x=10;' r1test.rb
$

--
Posted via http://www.ruby-....

Nobuyoshi Nakada

2/14/2009 6:21:00 AM

0

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

--
Nobu Nakada

7stud --

2/14/2009 7:52:00 AM

0

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


--
Posted via http://www.ruby-....

7stud --

2/14/2009 8:05:00 AM

0

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.
--
Posted via http://www.ruby-....

Nobuyoshi Nakada

2/14/2009 9:49:00 AM

0

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

--
Nobu Nakada

Roger Pack

2/14/2009 6:09:00 PM

0


> 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
--
Posted via http://www.ruby-....