How to I launch a ruby script from the command line by just its name?
Asked Answered
E

3

21

On windows, I can run my ruby script like this:

> ruby myscript.rb

but I want to set things up so that I can just do this instead?..

> myscript.rb

How do I do this? I know it's possible because I've recently moved from one PC that had this set up to a new PC that doesn't (yet).

Epicycloid answered 14/9, 2009 at 15:44 Comment(0)
Y
27

Associate the ".rb" file extension with the ruby interpreter. On Windows XP, one way to do this is to select "Tools|Folder options" in the file explorer, and to setup the association in the "File types" tab.

Another way would be to enter the following on the commandline which creates this file association for you:

assoc .rb=RubyScript
ftype RubyScript=ruby.exe %1 %*
York answered 14/9, 2009 at 15:48 Comment(4)
If this works, I'd listen to him over me as it sounds like he knows what he's talking about :)Juryrig
Damn, that was obvious. (Slaps own face)Epicycloid
For me, this did not work for some reason. I had to edit it manually into registry to HKEY_CLASSES_ROOT\Applications\ruby.exe\shell\open\commandHurley
This worked for me, after running the commands as administrator. Thanks!Eba
L
10

Read the bottom part of Wikipedia Ruby.

Windows

If you install the native Windows version of Ruby using the Ruby One-Click Installer, then the installer has setup Windows to automatically recognize your Ruby scripts as executables. Just type the name of the script to run it.

$ hello-world.rb
Hello world

If this does not work, or if you installed Ruby in some other way, follow these steps.

1. Log in as an administrator.
2. Run the standard Windows "Command Prompt", cmd.
3. At the command prompt (i.e. shell prompt), run the following Windows commands. When you run ftype, change the command-line arguments to correctly point to where you installed the ruby.exe executable on your computer.

$ assoc .rb=RubyScript
.rb=RubyScript

$ ftype RubyScript="c:\ruby\bin\ruby.exe" "%1" %*
RubyScript="c:\ruby\bin\ruby.exe" "%1" %*

For more help with these commands, run "help assoc" and "help ftype".

Lacuna answered 14/9, 2009 at 15:50 Comment(1)
A little copy/paste wouldn't have killed you, but this is exactly what I was looking for. Thanks. ;-)Braiding
N
1

None of the command line solutions worked for me on Windows 10. I've previously used the Windows GUI to open .rb files using Notepad++. When I run

script.rb

in the command line, it just opens Notepad++ with the .rb file (even after the command line fixes).

What solved the issue for me was

  1. Right click a .rb file
  2. Click "Open With"
  3. Click "Choose another app"
  4. Click "More apps"
  5. Click "Look for another app on this PC"
  6. Open the ruby.exe interpreter from my ruby installation.
Nullification answered 7/12, 2016 at 18:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.