How to run a program (Newbie)

Another newbie question here:

So: I’ve installed ruby. I’ve got the command prompt set up. If I run
“puts 1” it returns “1” and “puts 1+2” returns 3. All seems good.

Then I use SciTE to write a program called “calc.rb” The only line in
“calc.rb” is “puts 1+2” and I expect that it will return “3”

I get to the command prompt, which says “C:\Users\Otto>”

I type “irb” to get into the ruby prompt, changing the prompt to
“irb:001:0>”

The question is: how do I run the calc.rb program?

My tutorial book says that I should type “ruby calc.rb” but this returns
"NameError: undefined local variable or method ‘calc’ for main:Object
from :1

What am I doing wrong?

Hi,

You can do it like this:

Open windows command line interface then type “ruby
C:\Users\Otto\the\path\to\your\ruby\file”

When navigating to your ruby file, you can use tab key to automatically
complete the path.

Hope it helps.

On 11/19/2011 10:09 PM, Otto D. wrote:

I type “irb” to get into the ruby prompt, changing the prompt to
“irb:001:0>”

The question is: how do I run the calc.rb program?

My tutorial book says that I should type “ruby calc.rb” but this returns
"NameError: undefined local variable or method ‘calc’ for main:Object
from :1

What am I doing wrong?

The command your book tells you to run should not be performed in irb.
It should be performed at the command prompt you saw before you ran irb.
In addition, your calc.rb file needs to be saved to the same directory
that your command prompt is indicating to you.

That command tells the command processor to run a program named “ruby”
and to pass the string “calc.rb” as an argument. This is a pretty basic
usage of the command processor for Windows, and you may find it helpful
to learn a bit about the command processor itself while you’re learning
Ruby.

It seems that the book you’re using is pretty vague about these kinds of
specifics since you didn’t know about the need to run irb the other day
when you wanted to do “puts 1+2”. What is the title of this book, and
have you considered looking for a different book?

Even if the book is really good for other people, maybe you just need an
author that speaks to you better. There are many people here who can
help you find other reference material that might be easier for you to
use. :slight_smile:

-Jeremy

Thank you ToMaTo and Jeremy. Jeremy your explaination worked. I copied
my folder “programs” into C:\Users\Otto and from the command prompt:

C:\Users\Otto>

I typed cd programs and then typed “ruby calc.rb” and it returned 3!!!

Yay I ran my first program.

I would LOVE any suggestions for “teach yourself to program” people with
no background in coding. Currently I’m using “Learn To Program, 2nd
Edition” by Chris P… It’s from The Pragmatic Programmer’s series and
was recommended to me by a friend.

The thing that is annoying to me is that this stuff isn’t hard - but the
book should tell you specifically where to create the programs
directory. And it doesn’t. I put it on my desktop, which seems to be why
I was getting errors all night last night…

Anyhow thank you guys for helping me learn. I really appreciate it.

Learn to Program is a spectacular book, you’re in good hands.

Try oldkingjames.org it has Ruby lessons for complete beginners and it
is free!

On Nov 19, 2011, at 11:05 PM, Otto D. wrote:

The thing that is annoying to me is that this stuff isn’t hard - but the
book should tell you specifically where to create the programs
directory. And it doesn’t. I put it on my desktop, which seems to be why
I was getting errors all night last night…

The thing to remember is the book is OS agnostic, what may work for you
on a Windows computer may not work for somebody in Mac OS X, or Linux.
Where to place files is left to your discretion. Even placing them on
the desktop shouldn’t have been an issue. However, when running any
application the OS has to know the path to the file. In the case of
Windows, the desktop is a folder that is located in the following path:

C:\Users<your username>\Desktop\

If you were to run ruby, you would either have to first change to that
directory or include the path when you ran Ruby, like this:

ruby C:\Users<your username>\Desktop\calc.rb

that gets to be a bit unwieldy at times, so you’re probably better off
doing this;

C:>CD C:\Users<your username>\Desktop\

C:\Users<your username>\Desktop>

Then you can just use ruby calc.rb from the command prompt.

As Steve mentioned, Chris P.'s book is a great place to start.

Wayne


Get your facts first, and then you can distort them as much as you
please.

  • Mark Twain

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs