# How to require more than one thing from the command line

**URL:** <https://www.ruby-forum.com/t/how-to-require-more-than-one-thing-from-the-command-line/158563>\
**Category:** Ruby\
**Created:** [January 24, 2009, 9:41pm UTC](https://www.ruby-forum.com/t/how-to-require-more-than-one-thing-from-the-command-line/158563 "2009-01-24T21:41:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [January 24, 2009, 9:41pm UTC](https://www.ruby-forum.com/t/how-to-require-more-than-one-thing-from-the-command-line/158563/1 "2009-01-24T21:41:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [February 14, 2009, 6:19am UTC](https://www.ruby-forum.com/t/how-to-require-more-than-one-thing-from-the-command-line/158563/2 "2009-02-14T06:19:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [February 14, 2009, 7:11am UTC](https://www.ruby-forum.com/t/how-to-require-more-than-one-thing-from-the-command-line/158563/3 "2009-02-14T07:11:18Z")

</div>

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:in`load’  
from -e:1

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [February 14, 2009, 7:13am UTC](https://www.ruby-forum.com/t/how-to-require-more-than-one-thing-from-the-command-line/158563/4 "2009-02-14T07:13:58Z")

</div>

…and this does nothing:

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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [February 14, 2009, 7:22am UTC](https://www.ruby-forum.com/t/how-to-require-more-than-one-thing-from-the-command-line/158563/5 "2009-02-14T07:22:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [February 14, 2009, 8:53am UTC](https://www.ruby-forum.com/t/how-to-require-more-than-one-thing-from-the-command-line/158563/6 "2009-02-14T08:53:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [February 14, 2009, 9:06am UTC](https://www.ruby-forum.com/t/how-to-require-more-than-one-thing-from-the-command-line/158563/7 "2009-02-14T09:06:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [February 14, 2009, 10:51am UTC](https://www.ruby-forum.com/t/how-to-require-more-than-one-thing-from-the-command-line/158563/8 "2009-02-14T10:51:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [February 14, 2009, 7:10pm UTC](https://www.ruby-forum.com/t/how-to-require-more-than-one-thing-from-the-command-line/158563/9 "2009-02-14T19:10:30Z")

</div>

> ## 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
