# Newbie Questions

**URL:** <https://www.ruby-forum.com/t/newbie-questions/64136>\
**Category:** Ruby\
**Created:** [June 22, 2006, 11:30am UTC](https://www.ruby-forum.com/t/newbie-questions/64136 "2006-06-22T11:30:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![azalar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e0b2c6/32.png) [@azalar](https://www.ruby-forum.com/u/azalar)\
**Post date:** [June 22, 2006, 11:30am UTC](https://www.ruby-forum.com/t/newbie-questions/64136/1 "2006-06-22T11:30:05Z")

</div>

Hi,  
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  
chosen.

if menuChoiceString == “1” then  
puts “You pressed 1”  
else  
puts “You pressed 2”  
end

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  
failing.  
Can anyone help me understand why this would fail?

thanks

Paul

---

<div class="post-metadata">

**Author:** ![azalar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e0b2c6/32.png) [@azalar](https://www.ruby-forum.com/u/azalar)\
**Post date:** [June 22, 2006, 11:45am UTC](https://www.ruby-forum.com/t/newbie-questions/64136/2 "2006-06-22T11:45:29Z")

</div>

On Jun 22, 2006, at 2:30 AM, Paul T. wrote:

> for input with the gets method like this…  
> end
> 
> When i use this and enter 1 it goes to the second option.

irb(main):016:0\> x = gets  
1  
=\> “1\n”  
irb(main):017:0\> x  
=\> “1\n”  
irb(main):018:0\> x.strip  
=\> “1”  
irb(main):019:0\>

– Elliot T.

> **[Curiosity Blog – Elliot Temple](https://www.curi.us/blog/)**
>
> A philosophy blog

---

<div class="post-metadata">

**Author:** ![azalar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e0b2c6/32.png) [@azalar](https://www.ruby-forum.com/u/azalar)\
**Post date:** [June 22, 2006, 11:56am UTC](https://www.ruby-forum.com/t/newbie-questions/64136/3 "2006-06-22T11:56:10Z")

</div>

p “Please input your choice:”  
menuinput=gets

if menuinput.chomp! ==“1”  
p “1”  
else  
p “other”  
end

2006/6/22, Paul T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![azalar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e0b2c6/32.png) [@azalar](https://www.ruby-forum.com/u/azalar)\
**Post date:** [June 22, 2006, 11:48am UTC](https://www.ruby-forum.com/t/newbie-questions/64136/4 "2006-06-22T11:48:49Z")

</div>

gets will return a string with the carriage return included… that is  
menuChoiceString will be equal to “1\n” instead of simply “1” and that  
is why  
the comparison fails.

instead do this

if menuChoiceString.chop == “1” then

else

end

Horacio

æ?¨æ??æ?¥ 22 6æ?? 2006 18:30ã?Paul T. ã?ã??ã¯æ?¸ãã¾ã?ã?:

---

<div class="post-metadata">

**Author:** ![azalar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e0b2c6/32.png) [@azalar](https://www.ruby-forum.com/u/azalar)\
**Post date:** [June 22, 2006, 12:02pm UTC](https://www.ruby-forum.com/t/newbie-questions/64136/5 "2006-06-22T12:02:19Z")

</div>

you did well to look at the FAQ but I suppose that the solution is not  
there.  
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  
not  
know irb

Because

irb(main):016:0\> x = gets

> 1  
> =\> “1\n”  
> irb(main):017:0\> x  
> =\> “1\n”

“1\n” == “1”  
=\> false  
and menuChoiceString contains “1\n” not “1”

BTW you might like this idiom

case menuChoiceString.chomp  
when “1”  
do something clever  
when “2”  
do something smart  
else  
do something fancy  
end

irb(main):018:0\> x.strip

> =\> “1”  
> irb(main):019:0

#strip is a good alternative to chomp , because it deletes all heading  
and  
trailing whitespace.  
If on the other hand heading and trailing whitespace is meaningful in  
the  
input be careful to  
use #chomp

– Elliot T.

> [Curiosity Blog – Elliot Temple](http://www.curi.us/blog/)

If there are more question on this topic feel free to ask off list too.

Hopefully I was clear enough (time is a constraint in my job ☹ )

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![azalar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e0b2c6/32.png) [@azalar](https://www.ruby-forum.com/u/azalar)\
**Post date:** [June 22, 2006, 12:12pm UTC](https://www.ruby-forum.com/t/newbie-questions/64136/6 "2006-06-22T12:12:19Z")

</div>

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.
