Forum: Ruby Ruby command line options s and S

Posted by Love U Ruby (my-ruby)
on 2013-02-13 15:58
Can anyone help me to understand the difference between s and S with
small snippets?


Thanks
Posted by Joel Pearson (virtuoso)
on 2013-02-13 16:27
-s and -S are not related to each other.

http://www.ruby-doc.org/docs/ProgrammingRuby/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''.
Posted by "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-02-13 16:34
(Received via mailing list)
On Wed, Feb 13, 2013 at 3:59 PM, Love U Ruby <lists@ruby-forum.com> 
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.
Posted by Love U Ruby (my-ruby)
on 2013-02-13 16:51
"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post 
#1096683:
> On Wed, Feb 13, 2013 at 3:59 PM, Love U Ruby <lists@ruby-forum.com>
> 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
Posted by "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-02-13 17:03
(Received via mailing list)
On Wed, Feb 13, 2013 at 4:51 PM, Love U Ruby <lists@ruby-forum.com> 
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.
Posted by Love U Ruby (my-ruby)
on 2013-02-13 17:07
"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post 
#1096690:
> On Wed, Feb 13, 2013 at 4:51 PM, Love U Ruby <lists@ruby-forum.com>
> 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
Posted by WILLS, JAMAL A (Guest)
on 2013-02-13 17:10
(Received via mailing list)
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
Posted by Love U Ruby (my-ruby)
on 2013-02-13 17:19
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?
Posted by Love U Ruby (my-ruby)
on 2013-02-13 19:22
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
Posted by Love U Ruby (my-ruby)
on 2013-02-13 20:48
please help me here to understand me confusion. :(
Posted by unknown (Guest)
on 2013-02-13 20:58
(Received via mailing list)
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'
Posted by unknown (Guest)
on 2013-02-13 21:00
(Received via mailing list)
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.
Posted by Love U Ruby (my-ruby)
on 2013-02-13 21:08
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
Posted by Love U Ruby (my-ruby)
on 2013-02-13 21:12
>
> 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
Posted by unknown (Guest)
on 2013-02-14 01:03
(Received via mailing list)
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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.