Ruby command line options s and S

Can anyone help me to understand the difference between s and S with
small snippets?

Thanks

-s and -S are not related to each other.

http://www.ruby-doc.org/docs/ProgrammingRuby/html/rubyworld.html

-S
’ Looks for the program file using RUBYPATH or PATH environment
variable.

-s
’ Any command line switches found after the program filename, but before
any filename arguments or before a --, are removed from ARGV and set to
a global variable named for the switch. In the following example, the
effect of this would be to set the variable $opt to ``electric’'.

On Wed, Feb 13, 2013 at 3:59 PM, Love U Ruby [email protected]
wrote:

Can anyone help me to understand the difference between s and S with
small snippets?

-S tells Ruby to look into the $PATH variable when searching for the
script you want to run.
For example, say you have a test.rb in /home/user/bin/test.rb
and $PATH includes that folder. You could call ruby -S test.rb from
anywhere and Ruby will get test.rb from /home/user/bin

-s tells Ruby to treat switches after the script name in a special
way. They are removed from ARGV and used to create and set a value for
global variables with that name. For example:

$ cat test.rb
puts “variable is: #{$abc}”

$ ruby -s test.rb -abc=35
variable is: 35

Jesus.

On Wed, Feb 13, 2013 at 4:51 PM, Love U Ruby [email protected]
wrote:

and $PATH includes that folder. You could call ruby -S test.rb from
anywhere and Ruby will get test.rb from /home/user/bin

@Jesus - nice explanation you have given. Could you tell me how can I
see the $PATH contents in UBUNTU ?

Well, this is basic unix usage:

$ echo $PATH

Jesus.

“Jesús Gabriel y Galán” [email protected] wrote in post
#1096683:

On Wed, Feb 13, 2013 at 3:59 PM, Love U Ruby [email protected]
wrote:

Can anyone help me to understand the difference between s and S with
small snippets?

-S tells Ruby to look into the $PATH variable when searching for the
script you want to run.
For example, say you have a test.rb in /home/user/bin/test.rb
and $PATH includes that folder. You could call ruby -S test.rb from
anywhere and Ruby will get test.rb from /home/user/bin

@Jesus - nice explanation you have given. Could you tell me how can I
see the $PATH contents in UBUNTU ?

Thanks

You can see the contents of the variable using echo:

echo $PATH

If you want to see the contents of the directories, you can convert the
colons ‘:’ to spaces and look into each directory. A quick way would be
to use one of the following commands.

ls $(echo $PATH | tr ‘:’ ’ ') | less
find $(echo $PATH | tr ‘:’ ’ ') | less

If you want to get fancy, try:

find $(echo $PATH | tr ‘:’ ’ ') -type f | sort -u | xargs ls -l | less

This will eliminate duplicate entries and give you more details on each
file.

Note: $PATH directories often have many files, so I recommend using
either grep, redirecting to a file, or using less as shown above.

Jamal Wills

WILLS, JAMAL A wrote in post #1096692:

You can see the contents of the variable using echo:

echo $PATH

If you want to see the contents of the directories, you can convert the
colons ‘:’ to spaces and look into each directory. A quick way would be
to use one of the following commands.

ls $(echo $PATH | tr ‘:’ ’ ') | less
find $(echo $PATH | tr ‘:’ ’ ') | less

Thanks @Jamal - but why $RUBYPATH is showing nothing?

Another confusion here I am having

I have wrote code as below :

++++++++++++++++++++++++++++++
#!/home/peter/script ruby

puts “hello world”

++++++++++++++++++++++++++++++

Now when I am running it from the “/home/peter/”

$ test.rb

giving error as “bash: /home/peter/script/commandoptionstest.rb:
Permission denied”

$ ruby test.rb

giving error as “ruby: No such file or directory –
commandoptionstest.rb (LoadError)”

Then what the purpose of “#!/home/peter/script ruby” - help me to
understand.

Thanks

please help me here to understand me confusion. :frowning:

“Jesús Gabriel y Galán” [email protected] wrote in post
#1096690:

On Wed, Feb 13, 2013 at 4:51 PM, Love U Ruby [email protected]
wrote:

and $PATH includes that folder. You could call ruby -S test.rb from
anywhere and Ruby will get test.rb from /home/user/bin

@Jesus - nice explanation you have given. Could you tell me how can I
see the $PATH contents in UBUNTU ?

Well, this is basic unix usage:

$ echo $PATH

Jesus.

Yeah,I know I tried that also.

peter@ubuntu:~$ echo $RUBYPATH

peter@ubuntu:~$ echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
peter@ubuntu:~$

But $PATH gives me too many. How to recognize which one is for Ruby?
That’s my confusion.

BTW why $RUBYPATH is showing nothing?

Thanks

Am 13.02.2013 19:22, schrieb Love U Ruby:

commandoptionstest.rb (LoadError)"

Then what the purpose of “#!/home/peter/script ruby” - help me to
understand.

Thanks

Please google ‘ruby shebang’

unknown wrote in post #1096746:

Am 13.02.2013 17:19, schrieb Love U Ruby:

find $(echo $PATH | tr ‘:’ ’ ') | less

Thanks @Jamal - but why $RUBYPATH is showing nothing?

Because it’s empty or not defined?

Which one is not defined as per your doubt?

Thanks

giving error as “ruby: No such file or directory –
commandoptionstest.rb (LoadError)”

It is “test.rb” not “commandoptionstest.rb”. By mistake I wrote that
name.

Then what the purpose of “#!/home/peter/script ruby” - help me to
understand.

Thanks

Am 13.02.2013 21:08, schrieb Love U Ruby:

Which one is not defined as per your doubt?

Thanks

Are you kidding???

$RUBYPATH is probably not defined / set.

Did you try `$ env’ as I suggested?
Search in the output for RUBYPATH, it’s probably not there.
You can also use

$ env | grep RUBYPATH

Am 13.02.2013 17:19, schrieb Love U Ruby:

find $(echo $PATH | tr ‘:’ ’ ') | less

Thanks @Jamal - but why $RUBYPATH is showing nothing?

Because it’s empty or not defined?

Use

$ env

to show all available environment information.