Can you run a command line script with arguments, without typing 'ruby' first?

Hi All,

I would like to do this -> my_script arg1, arg2
This doesn’t seem to work when the script has arguments.

But if I type -> ruby my_script arg1,arg2
It works fine.

Is there a way I can run the my_script without typing ruby each time?
Thanks

chmod a+x my_script (restrict permissions if you want)
./my_script args

or put my_script in your PATH, and just type `my_script args’

On Mon, Nov 24, 2008 at 4:47 PM, Jayson W.

On Nov 24, 2008, at 11:47 AM, Jayson W. wrote:

Hi All,

I would like to do this -> my_script arg1, arg2
This doesn’t seem to work when the script has arguments.

But if I type -> ruby my_script arg1,arg2
It works fine.

Is there a way I can run the my_script without typing ruby each time?

on any *nix platform, make the script executable and put a hash-bang
line at the top

chmod 755 my_script
vim my_script
i#!/usr/bin/env ruby:wq
my_script arg1 arg2

Blessings,
TwP

#!/usr/bin/env ruby

Yes, I forgot the shebang.

Diogo

I am using win os, so the shabang thing isn’t an option for me. I put
the script in my ruby bin path, and I can access the script from
anywhere now, but I still have the same problem with the script not
running properly unless i type ruby first. It is as if ruby does not
attempt to read in arguments unless you explicitly pass the script
through ruby. If I call the script with the arguments without putting
‘ruby’ first, the args don’t seem to get read.

On Mon, Nov 24, 2008 at 2:12 PM, Jayson W.
[email protected]wrote:

wrote:

This doesn’t seem to work when the script has arguments.


Diogo

Yep, in Windows you have to invoke ruby first on the command line. The
shebang in the Unix world is kind of a short hand for doing this.


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

On Mon, Nov 24, 2008 at 2:21 PM, Glen H. [email protected]
wrote:

through ruby. If I call the script with the arguments without putting

On Mon, Nov 24, 2008 at 4:47 PM, Jayson W.

Thanks

Yep, in Windows you have to invoke ruby first on the command line. The
shebang in the Unix world is kind of a short hand for doing this.


“Hey brother Christian with your high and mighty errand, Your actions speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

If you absolutely must get rid of the need to type ruby first you could
use
rubyscript2exe to build a windows exe file out of your script.


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

On Mon, Nov 24, 2008 at 2:26 PM, Kyle S.
[email protected]wrote:

Jayson, if your not on a real OS, you need to associate the extension
.rb with the interpreter. It’s been a little while since I’ve done
this in windows, but it’s actually pretty straightforward. You may
have to google for how to associate the script with the interpreter
though, as I don’t recall the exact steps.

–Kyle

And here I am forgetting the simple steps, doh!


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

The associations are correct. I reset them just to be sure though. I
can execute the script without ruby, and it runs, but without ruby
first, the args are not getting read.

The rubyscript2exe suggestion worked. Thanks Glen

Thanks everyone for the feedback

On Mon, Nov 24, 2008 at 3:12 PM, Jayson W.
[email protected] wrote:

I am using win os, so the shabang thing isn’t an option for me. I put
the script in my ruby bin path, and I can access the script from
anywhere now, but I still have the same problem with the script not
running properly unless i type ruby first. It is as if ruby does not
attempt to read in arguments unless you explicitly pass the script
through ruby. If I call the script with the arguments without putting
‘ruby’ first, the args don’t seem to get read.

Jayson, if your not on a real OS, you need to associate the extension
.rb with the interpreter. It’s been a little while since I’ve done
this in windows, but it’s actually pretty straightforward. You may
have to google for how to associate the script with the interpreter
though, as I don’t recall the exact steps.

–Kyle

Jayson W. wrote:

I am using win os, so the shabang thing isn’t an option for me. I put
the script in my ruby bin path, and I can access the script from
anywhere now, but I still have the same problem with the script not
running properly unless i type ruby first. It is as if ruby does not
attempt to read in arguments unless you explicitly pass the script
through ruby. If I call the script with the arguments without putting
‘ruby’ first, the args don’t seem to get read.

With my one-click-install it works just fine. This is my test1.rb:

puts ARGV

#when called like this:

#test1.rb first second

#it produces

#first
#second

hth,

Siep

On Mon, Nov 24, 2008 at 3:39 PM, Jayson W.
[email protected] wrote:

The associations are correct. I reset them just to be sure though. I
can execute the script without ruby, and it runs, but without ruby
first, the args are not getting read.

The rubyscript2exe suggestion worked. Thanks Glen

Thanks everyone for the feedback
Jayson, hum. I guess that makes sense. Now that I’m thinking back I
never used command line options on my windows-ruby stuff, and I do
recall having to go through extra (extra ugly too) hoops to get some
of my vbscripts to read options from the command line properly. The
rubyscript2exe is probably the best way to go anyway :slight_smile:

2008/11/24 Kyle S. [email protected]:

Jayson, hum. I guess that makes sense. Now that I’m thinking back I
never used command line options on my windows-ruby stuff, (…)

FWIW, I never had problems calling Ruby scripts with command line
args, and I just enter the script name without “ruby”. Currently I’m
using the OCI on Windows 2000…

Regards,
Pit

How about creating a .bat file with:

ruby my_script %*

At least, I think it’s %*.

Glen H. wrote:

If you absolutely must get rid of the need to type ruby first you could use
rubyscript2exe to build a windows exe file out of your script.

You can associate .rb files with the Ruby Interpreter - that should do
it. That’s how One Click Install sets it up so that if you double click
on it, it will just run the script rather than open it in an editor.

Cheers,
Mohit.
11/25/2008 | 1:10 PM.

Pit C. wrote:

2008/11/24 Kyle S. [email protected]:

Jayson, hum. I guess that makes sense. Now that I’m thinking back I
never used command line options on my windows-ruby stuff, (…)

FWIW, I never had problems calling Ruby scripts with command line
args, and I just enter the script name without “ruby”. Currently I’m
using the OCI on Windows 2000…

I think the steps should be something like:

  1. Add Ruby Interpreter path to your path to make sure that you can run
    Ruby from anywhere
  2. In Explorer, go to a folder that has a Ruby script ending with an
    extension of .rb
  3. Press shift and right click on the .rb file there
  4. In the context menu, it should give an option called ‘Open with’ - if
    that has the Ruby interpreter in it, click on it. If that works, great!
  5. Alternatively, click on ‘Choose Program’ - navigate to your ruby
    install and select ruby.exe
  6. Click on ‘Always use the selected program’ - say OK, etc.
  7. Go to a command prompt and try to run your ruby script without ruby
    in the name and with the parameters
    command>myscript.rb param1
    It should work.

Note that the associations need to be set up in the GUI and the script
must be run from the command line with the parameters. It has worked
like that for me for months. Do post if there’s a problem.

I think using Rubyscript2exe for this is a bit of a troublesome way -
since you may want to change your script, etc. and having to repackage
it all the time just to try something simple is a pain!

Cheers,
Mohit.
11/25/2008 | 1:18 PM.

Mohit,

I thought this would work also, but for some reason it is not. I
currently can run any ruby script from anywhere. The only issue I have
is when its a script run from the command line that needs args. The
script still runs, but I can tell that the args are not being read. If
I run the same script with ruby, it works fine. The only things I can
think of that might be causing this are:

  1. ruby may think that the arguments are meant for the interpreter
    instead of my script. So they are being consumed by the interpreter
    and my script never sees them. Is there a way to tell ruby the the
    args are not for the interpreter?
  2. I am using the global array ARGV[x] to read in command line
    arguments. Is there a better method.

The rubyscript2exe method is working, but is a bit cumbersome when I
have to make small changes. Being able to run the script with args
directly would be best for me.

Thanks Mohit!

Jayson W. wrote:

The associations are correct. I reset them just to be sure though. I
can execute the script without ruby, and it runs, but without ruby
first, the args are not getting read.

Are command-line arguments included in the file association?
There should be something like “…\ruby.exe” “$*” or
“…\ruby.exe” “%1” “%2” “%3” “%4” “%5” (up to %9, this form is
restricted to 9 arguments).

Daniel

Daniel,
I don’t believe these arguments are included in the association. I am
not sure how to add them.

On Tue, Nov 25, 2008 at 12:00 PM, Daniel Schömer

Doesn’t seem to work that way for me. I created a file called args.rb
that looks like this

puts ARGV[0]
#end

This is what I get

args.rb hello
nil

ruby args.rb hello
hello

I think in the first example ruby is using my arguments for the
interpreter. Any ideas how I can tell ruby to use the arguments for
the script and not the interpreter?

Jayson