Forum: Ruby Console input

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
43360de7500b5cc05e858b8b2a1f5069?d=identicon&s=25 Michal (Guest)
on 2006-06-07 07:58
I'd ask you why the following code doesn't works:
--
data = gets
data.chomp

if data == "a"
	puts "True"
else
	puts "False"
end
--
It's always false. (ruby 1.8.4 in WinXP console)
Thanks for your answers.
0b561a629b87f0bbf71b45ee5a48febb?d=identicon&s=25 Dave Burt (Guest)
on 2006-06-07 08:25
(Received via mailing list)
Michal wrote:
> --
> It's always false. (ruby 1.8.4 in WinXP console)

"chomp" does not alter the string, it returns a new, modified copy.
"chomp!" does modify the string, and returns nil if no change was
made.

Try these instead:

  data = gets.chomp

or:

  data = gets
  data.chomp!

Cheers,
Dave
This topic is locked and can not be replied to.