A gets problem

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.

On 2009-12-09, Ruby N. [email protected] wrote:

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

On Wed, Dec 9, 2009 at 3:35 PM, Seebs [email protected] wrote:

On 2009-12-09, Ruby N. [email protected] wrote:

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.

How to re-gets it then?
Thanks.

On Wed, Dec 9, 2009 at 3:41 PM, Ruby N. [email protected]
wrote:

Thanks.

Why don’t you just use ENTER instead of 2 CTRL-D-s when you are entering
the
line?

Tamas

On 2009-12-09, Ruby N. [email protected] wrote:

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

On Wed, Dec 9, 2009 at 4:06 PM, Tamas S. [email protected] wrote:

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?

why not?

On Wed, Dec 9, 2009 at 3:45 PM, Seebs [email protected] wrote:

On 2009-12-09, Ruby N. [email protected] wrote:

How to re-gets it then?

No clue. Â Why 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 = gets
hello
=> “hello\n”
irb(main):002:0> x = gets
world
=> “world\n”

Ruby N. wrote:

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.

-Justin

On Wed, Dec 9, 2009 at 6:40 PM, Justin C. [email protected]
wrote:

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.

On 2009-12-09, Ruby N. [email protected] wrote:

On Wed, Dec 9, 2009 at 3:45 PM, Seebs [email protected] wrote:

On 2009-12-09, Ruby N. [email protected] wrote:

How to re-gets it then?

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

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