Running a .rb file under Windows (beginner's question)

I just installed Ruby on a Windows XP machine. At the start of the
path is:
C:\Ruby192\bin;

I created
C:\Documents and Settings\brazee\My Documents\Code\Ruby\Hello.rb

How do I run it? When I look at it in explorer, it has a Ruby icon.

I cut and pasted a ruby program from the Web - but I suspect the first
line doesn’t fit this environment.

(I’m going to try at home on my Mac and expect I’ll have to make a
change to that line there as well).


“In no part of the constitution is more wisdom to be found,
than in the clause which confides the question of war or peace
to the legislature, and not to the executive department.”

  • James Madison

Assuming you installed Ruby using the RubyInstaller package, look in
the Start Menu for Ruby 1.9.2-p0 and select “Start command prompt with
Ruby”. Now change directory (CD) to where your Hello.rb file resides;
i.e., type this and hit return:

cd “C:\Documents and Settings\brazee\My Documents\Code\Ruby”

Now type Hello.rb and you should get the output of your program.
Alternatively, you can type: ruby Hello.rb

Hope that helps.

Charles

On Thu, 16 Sep 2010 12:27:54 -0500, Charles R.
[email protected] wrote:

Hope that helps.
Thanks. That worked. I didn’t have to change the 1st line. I
wonder if it ran when I clicked on it from the Windows GUI - except it
ran and finished without me seeing anything in the text program.

How do I run that same program when I copy it to my Mac?


“In no part of the constitution is more wisdom to be found,
than in the clause which confides the question of war or peace
to the legislature, and not to the executive department.”

  • James Madison

On 16 September 2010 18:45, Howard B. [email protected] wrote:

Thanks. Â That worked. Â I didn’t have to change the 1st line. Â I
wonder if it ran when I clicked on it from the Windows GUI - except it
ran and finished without me seeing anything in the text program.

Yes, that is almost certainly what happened.

How do I run that same program when I copy it to my Mac?

You do more or less the same thing, only on the Mac you’d open
Terminal, then cd to wherever you place the file. Note, however, that
on a Mac you’d use forward slashes instead of backslashes in the path;
e.g,

cd ~/Code/Ruby/

Charles

On Thu, 16 Sep 2010 13:11:36 -0500, Charles R.
[email protected] wrote:

How do I run that same program when I copy it to my Mac?

You do more or less the same thing, only on the Mac you’d open
Terminal, then cd to wherever you place the file. Note, however, that
on a Mac you’d use forward slashes instead of backslashes in the path;
e.g,

cd ~/Code/Ruby/

I opened terminal in that directory (using Path Finder), and tried the
following:

cd “/C:/Documents and Settings/brazee/My Documents/code/Ruby”
Last login: Thu Sep 16 18:53:59 on ttys000
Kernel Information: Darwin 10.4.0 i386
\e[0;33mGNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)
Copyright (C) 2007 Free Software Foundation, Inc.
\e[1;30mUptime: 18:55 up 4 days, 1:16, 3 users, load averages: 0.22
0.25 0.37
\e[1;30mServer time is: Thu Sep 16 18:55:01 MDT 2010
~ > cd “/C:/Documents and Settings/brazee/My Documents/code/Ruby”
/C:/Documents and Settings/brazee/My Documents/code/Ruby > ls
99Bottles.rb 99BottlesR.rb Hello.rb
/C:/Documents and Settings/brazee/My Documents/code/Ruby > hello.rb
-bash: hello.rb: command not found
/C:/Documents and Settings/brazee/My Documents/code/Ruby > Hello.rb
-bash: Hello.rb: command not found
/C:/Documents and Settings/brazee/My Documents/code/Ruby >

(Yes, that is on my Mac - I synchronize some documents between work
and home by having that weird path).


“In no part of the constitution is more wisdom to be found,
than in the clause which confides the question of war or peace
to the legislature, and not to the executive department.”

  • James Madison

On Sep 16, 9:57 pm, Howard B. [email protected] wrote:

cd ~/Code/Ruby/
0.25 0.37
(Yes, that is on my Mac - I synchronize some documents between work
and home by having that weird path).

hello.rb is not marked as executable (chmod +x) to be invoked
directly.

Also, you need to invoke from the same directory like “./hello.rb”
since “.” is not in the PATH.

Avoid troubles and invoke your scripts always preceding with ruby:

ruby hello.rb

That works across platforms.