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.

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 2009-12-09, Ruby Newbee <rubynewbee@gmail.com> wrote:

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

--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
| Seebs.Net <-- lawsuits, religion, and funny pictures
Fair game (Scientology) - Wikipedia <-- get educated!

How to re-gets it then?
Thanks.

···

On Wed, Dec 9, 2009 at 3:35 PM, Seebs <usenet-nospam@seebs.net> wrote:

On 2009-12-09, Ruby Newbee <rubynewbee@gmail.com> 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.

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 2009-12-09, Ruby Newbee <rubynewbee@gmail.com> wrote:

How to re-gets it then?

--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
| Seebs.Net <-- lawsuits, religion, and funny pictures
Fair game (Scientology) - Wikipedia <-- get educated!

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

Tamas

···

On Wed, Dec 9, 2009 at 3:41 PM, Ruby Newbee <rubynewbee@gmail.com> wrote:

On Wed, Dec 9, 2009 at 3:35 PM, Seebs <usenet-nospam@seebs.net> wrote:
> On 2009-12-09, Ruby Newbee <rubynewbee@gmail.com> 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.

why not?

···

On Wed, Dec 9, 2009 at 4:06 PM, Tamas Szabo <szabtam@gmail.com> wrote:

On Wed, Dec 9, 2009 at 3:41 PM, Ruby Newbee <rubynewbee@gmail.com> wrote:

On Wed, Dec 9, 2009 at 3:35 PM, Seebs <usenet-nospam@seebs.net> wrote:
> On 2009-12-09, Ruby Newbee <rubynewbee@gmail.com> 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.

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

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"

···

On Wed, Dec 9, 2009 at 3:45 PM, Seebs <usenet-nospam@seebs.net> wrote:

On 2009-12-09, Ruby Newbee <rubynewbee@gmail.com> wrote:

How to re-gets it then?

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

Ruby Newbee wrote:

···

On Wed, Dec 9, 2009 at 4:06 PM, Tamas Szabo <szabtam@gmail.com> wrote:
  

On Wed, Dec 9, 2009 at 3:41 PM, Ruby Newbee <rubynewbee@gmail.com> wrote:

On Wed, Dec 9, 2009 at 3:35 PM, Seebs <usenet-nospam@seebs.net> wrote:
      

On 2009-12-09, Ruby Newbee <rubynewbee@gmail.com> 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.

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

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

···

On 2009-12-09, Ruby Newbee <rubynewbee@gmail.com> wrote:

On Wed, Dec 9, 2009 at 3:45 PM, Seebs <usenet-nospam@seebs.net> wrote:

On 2009-12-09, Ruby Newbee <rubynewbee@gmail.com> 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.

--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
| Seebs.Net <-- lawsuits, religion, and funny pictures
Fair game (Scientology) - Wikipedia <-- get educated!

oh~ thanks a lot, have got that.

···

On Wed, Dec 9, 2009 at 6:40 PM, Justin Collins <justincollins@ucla.edu> 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.