irb(main):001:0> x = gets
hahase=> “hahase”
irb(main):002:0> x
=> “hahase”
irb(main):003:0> x = gets
=> nil
irb(main):004:0> x = gets
=> nil
I run x = gets then it wait me for any input.
I input “hahase” following with two “ctrl + D”.
The gets finished, and it seems have got the corrent value.
But when I re-executed x = gets it always got a nil.
Why this happens? Thank you.
Why this happens? Thank you.
Why don’t you just use ENTER instead of 2 CTRL-D-s when you are entering the
line?
why not?
Mainly because gets is waiting for an EOL, not an EOF. Recall that, in
this case, gets is reading from stdin. Once you tell it stdin is at the
end of the file, it has no reason to continue reading from it.
Mainly because gets is waiting for an EOL, not an EOF. Recall that, in this
case, gets is reading from stdin. Once you tell it stdin is at the end of
the file, it has no reason to continue reading from it.
but if I inputed a ENTER rather than two “ctrl +D”, thing works fine.
Exactly.
Here is your problem, as it would appear in a newsgroup about credit
card accounts:
Credit Newbee: Help! I called my bank and told them to cancel
my card, now stores won’t take it.
Seebs: That is because you cancelled your card.
Credit Newbee: But if I just call them and ask about the balance,
and don’t tell them to cancel the card, it keeps working.
-s
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.