简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM