views:

197

answers:

3

This is probably a really silly question bu tI can't seem to find an answer since I'm apparently failing on keywords.

You know how you can run commands from the commandline in linux if you put a line on the top of your .rb file so you don't have to type ruby myfile.rb all the time and can just do ./myfile.rb ? Is it possible to have the same thing on a windows environment? I use git bash as my shell most of the time and have a lot of cygwin binaries installed. It's just that the general method is to put that line on top and then chmod the file, which isn't really an option.

A: 

May be you need to define regular windows file association?

Trickster
+2  A: 

Yes, you can:

assoc .rb=RubyScript
ftype RubyScript=ruby "%1" %*

After that you can simply call your ruby script like an executable program.

If you also want to be able to drop the .rb file extension, then you need to include .rb in the PATHEXT environment variable.

Joey
BTW, this is nothing script interpreter specific. This is just the normal file association stuff that is also used to associate, say Word with `.doc` files. Which means you can also do it the same way you would with a document: just double click on a Ruby file, the dialog will pop up asking you what to do with the file, browse to `ruby.exe` and you're done.
Jörg W Mittag
I just thought someone coming from Linux might be more comfortable with a command-line solution :-)
Joey
this doesnt seem to work completely on win7. while I can start a script, it doesnt seem to pass the command line to script. for example if I have a script that does "puts ARGV.size" and run foo.rb param1 it will output 0 :(
MikeJ
@Jörg, not exactly though, as that won't add the `%*` so you can't pass arguments to your scripts, then.
Joey
@MikeJ: Did you forget the `%*`?
Joey
@Johannes: Yes I typed as presented except that I included the full path to ruby in the ftype command : ftype RubyScript=c:\ruby19\bin\ruby.exe %1 %*. Do I need any of these things in quotes?
MikeJ
@MikeJ: The `%1` apparently. The `%*` definitely not.
Joey
thanks for the help. I found that I had inadvertantly set up a association in windows explorer that was overriding the startup of my script. to fix windows where I broke it I had to follow this article:http://www.mydigitallife.info/2009/11/05/how-to-unassociate-remove-or-delete-programs-from-open-with-or-recommended-programs-list/comment-page-1/
MikeJ
A: 

If you want a graphical tool to assist with this on Win 7, you might want to check out: http://defaultprogramseditor.com/

With this tool, you can easily edit the context menu items for .rb files (or any other files)

for the "open" command to be:

ruby "%1" %*

and you can create an "edit" command, such as:

c:\ruby\scite\scite.exe "%1"

As noted above, if you do not specify %* after "%1", your script will not see any of the command line arguments. And don't forget to add .RB to the PATHEXT environment variable.

CameraCarryingCoder