I am new to Ruby and have a newbie Ruby question that i couldn’t find
the answer to in the FAQ.
If this is the wrong forum to post newbie questions please accept my
apologies and direct me to a more suitable forum where i can ask newbie
Ruby questions. Thanks.
First i display a console based menu with puts statements and then wait
for input with the gets method like this…
menuChoiceString = gets
Then i have the following code to act based on what menu option was
if menuChoiceString == “1” then
puts “You pressed 1”
puts “You pressed 2”
When i use this and enter 1 it goes to the second option.
It looks fairly simple syntax so i’m not sure why the if statement is
Can anyone help me understand why this would fail?
you did well to look at the FAQ but I suppose that the solution is not
You did even better to post the question here and got a correct answer,
though maybe a little bit cryptic for a newcomer. Especially if you do
irb(main):016:0> x = gets
“1\n” == “1”
and menuChoiceString contains “1\n” not “1”
BTW you might like this idiom
do something clever
do something smart
do something fancy
#strip is a good alternative to chomp , because it deletes all heading
If on the other hand heading and trailing whitespace is meaningful in
input be careful to
Thanks for all your replies
Makes sense now that i’ve seen what is happening behind the scenes.
I come from a Java background but didn’t think to test if it was adding
carriage returns to the end of the string.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.