TmTm
TmTm

Reputation: 1

gets.chomp to match ENTER (in ruby)

I thought that (in a while...do loop)

    gets.chomp != ''

might match a carriage return from a terminal. It doesn't. What am I not understanding? Thank you.

Upvotes: 0

Views: 308

Answers (1)

ptierno
ptierno

Reputation: 10074

String#chomp removes carriage returns from the string it is being called on.

If you remove chomp it should give you the expected output. See below:

2.1.2 :001 > def foo
2.1.2 :002?>   while true do
2.1.2 :003 >       puts gets != ''
2.1.2 :004?>     end
2.1.2 :005?>   end
 => :foo
2.1.2 :006 > foo
a
true
b
true
c
true
1
true
2
true
# about to press enter
true

true

Hope this helps

Upvotes: 1

Related Questions