Newbie question what am I doing wrong?

I wrote this simple program but it won’t work. What am I doing wrong?

puts ‘Welcome to McDonald’s, can I take your order?’
order = gets.chomp

if order == yes
puts ‘Go ahead.’
else
puts ‘Look, I asked a simple yes or no question.’
puts ‘Yes, or no now, can I take your order?’
order = gets.chomp
end

When you ask a question and states that your program “won’t work”, it’s
not
very helpful. You should give us examples of it “not working” and what
the
expected output is.

In this case, I think the problem is with the “if order == yes” line –
it’s comparing order with a variable named yes, not with the
corresponding string. So the correct version would be:

if order == “yes”
puts ‘Go ahead.’


Carlos A.

Control engineering
Polytechnic School, University of So Paulo, Brazil
Computer engineering
Embry-Riddle Aeronautical University, USA

2013/1/6 Lee V. [email protected]

Thank you! Yes, that fixed the problem. In the future I will give more
details.

The problem I was having was that after an answer was typed in for the
first question the program would close. By putting ‘yes’ instead of
yes, the program ran fine. I added more steps to it after and the whole
program works, so I think I’m getting the hang of it.

On 7/01/2013, at 1:32 PM, “Lee V.” [email protected] wrote:

I wrote this simple program but it won’t work. What am I doing wrong?

What did you expect it to do? How does it not meet your expectations?

Henry

Didn’t the compiler give you and error of the likes of:
in <main>': undefined local variable or methodyes’ for
main:Object (NameError)
?
That clearly states that yes is being considered as a method or a
variable instead of a string.

You should pay attention to those, specially if you are just starting :wink:

On Sun, Jan 6, 2013 at 7:51 PM, Lee V. [email protected] wrote:

The problem I was having was that after an answer was typed in for the
first question the program would close. By putting ‘yes’ instead of
yes, the program ran fine. I added more steps to it after and the whole
program works, so I think I’m getting the hang of it.

I’m curious – are you running in windows, and starting the program
from explorer? The behaviour you describe makes me believe so. If that
is so, instead try running it from a command prompt so the window
won’t close when the program exits.

David G. wrote in post #1091257:

Didn’t the compiler give you and error of the likes of:
in <main>': undefined local variable or methodyes’ for
main:Object (NameError)
?
That clearly states that yes is being considered as a method or a
variable instead of a string.

You should pay attention to those, specially if you are just starting :wink:
That’s another problem I’m having. The screen closes before I get a
chance to read what it says. I’ve been using sleep(30) at the end of
all my programs so I can view the full result but if there’s an error,
the screen doesn’t sleep for me to see why.

I’m curious – are you running in windows, and starting the program
from explorer? The behaviour you describe makes me believe so. If that
is so, instead try running it from a command prompt so the window
won’t close when the program exits.

I’m using Intype to edit my programs, saving them as ruby files, and
then opening them. What is the command prompt? Somebody else gave me
advice to fix closing problem but it didn’t work and I just moved on
without figuring out why. I would greatly appreciate it if you could
tell me how to stop my screen from closing.

On Jan 7, 2013, at 1:41 AM, Lee V. wrote:

I’m curious – are you running in windows, and starting the program
from explorer? The behaviour you describe makes me believe so. If that
is so, instead try running it from a command prompt so the window
won’t close when the program exits.

Since you mentioned Windows, substitute command prompt for DOS prompt.
Once you open a DOS prompt, simply type:

irb

that’s the interactive Ruby interpreter. For more details see:
http://www.ruby-lang.org/en/documentation/quickstart/

于 2013-1-7 19:01, Wayne B. 写道:

Since you mentioned Windows, substitute command prompt for DOS prompt. Once you
open a DOS prompt, simply type:

irb

My win7 has no DOS, how to run ruby? thanks.

On Mon, Jan 7, 2013, at 12:07, Feng He wrote:

于 2013-1-7 19:01, Wayne B. 写道:

Since you mentioned Windows, substitute command prompt for DOS prompt. Once
you open a DOS prompt, simply type:

irb

My win7 has no DOS, how to run ruby? thanks.

Start menu > Run > Type ‘cmd’

Best Regards:

Zoltan

On Jan 7, 2013, at 5:07 AM, Feng He wrote:

于 2013-1-7 19:01, Wayne B. 写道:

Since you mentioned Windows, substitute command prompt for DOS prompt. Once you
open a DOS prompt, simply type:

irb

My win7 has no DOS, how to run ruby? thanks.

Sure it does.

Start > All Programs > Accessories > Command Prompt