a gets problem

R

Ruby Newbee

Hello,

Please see my operation below:


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.

Regards.
 
S

Seebs

But when I re-executed x = gets it always got a nil.
Why this happens? Thank you.

Because you convinced it that it had reached the end of file, and gets
returns a nil after you've reached end of file.

-s
 
R

Ruby Newbee

Because you convinced it that it had reached the end of file, and gets
returns a nil after you've reached end of file.

How to re-gets it then?
Thanks.
 
S

Seebs

How to re-gets it then?

No clue. Why not try, you know, NOT DOING THAT?

"Hey, this is weird, after I told my bank to close my account, my
card stopped working."

If you don't want Ruby to think a file has ended, don't send it EOF a lot.

-s
 
T

Tamas Szabo

[Note: parts of this message were removed to make it a legal post.]

How to re-gets it then?
Thanks.
Why don't you just use ENTER instead of 2 CTRL-D-s when you are entering the
line?

Tamas
 
R

Ruby Newbee

No clue. =C2=A0Why not try, you know, NOT DOING THAT?

but if I inputed a ENTER rather than two "ctrl +D", thing works fine.

irb(main):001:0> x =3D gets
hello
=3D> "hello\n"
irb(main):002:0> x =3D gets
world
=3D> "world\n"
 
J

Justin Collins

Ruby said:

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.

-Justin
 
R

Ruby Newbee

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.

oh~ thanks a lot, have got that.
 
S

Seebs

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,708
Messages
2,569,347
Members
44,654
Latest member
7Jhamal74

Latest Threads

Top