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 Agarie
Control engineering
Polytechnic School, University of São Paulo, Brazil
Computer engineering
Embry-Riddle Aeronautical University, USA
2013/1/6 Lee V. <lists@ruby-forum.com>
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
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.
Didn't the compiler give you and error of the likes of:
in `<main>': undefined local variable or method `yes' 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
···
On Sun 06 Jan 2013 06:51:17 PM MST, Lee V. 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.
···
On Sun, Jan 6, 2013 at 7:51 PM, Lee V. <lists@ruby-forum.com> 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.
Didn't the compiler give you and error of the likes of:
in `<main>': undefined local variable or method `yes' 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
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.
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.
于 2013-1-7 19:01, Wayne Brissette 写道:
> Since you mentioned Windows, substitute command prompt for DOS prompt. Once you open a DOS prompt, simply type:
>
> irb