Tugger the SLUGger!SLUG Mailing List Archives

Re: [coders][NRS] 1st night

david wrote:
On Fri, 2006-06-16 at 18:06 +1000, O Plameras wrote:
David wrote:

david@test:~/ruby $ irb
irb(main):012:0> puts $stdin.readlines.sort_by {rand}
3	<-- c^d here
=> nil
irb(main):013:0> puts $stdin.readlines.sort_by {rand} unless $stdin.eof
=> nil

In other words, it looks like you don't want the <unless $stdin.eof>,
because it seems to get eof before you get a chance to enter anything.
The reason for not being able to enter is because after you put
"irb" you must have put c^d before you put the command,

Well... actually, no. This was all interactive and I didn't try it from
a script. I just put the c^d to help indicate where the input ended and
the output started.

When I added <while not $stdin.eof> to the end, irb took me directly to
the next command prompt and didn't give me an opportunity to input
anything. Someone might be able to explain why.

Interestingly, I just tried again, and it turns out that the SECOND or
subsequent attempt at any of the following will fail:

puts $stdin.readlines.sort_by {rand} while not $stdin.eof
puts $stdin.readlines.sort_by {rand} unless $stdin.eof

but not if I do:

puts $stdin.readlines.sort_by {rand}

puts $stdin.readlines.sort_by {rand} unless $stdin.eof.

This is so, because the stored value is now EOF(nil) in $_.

So, after you put C^d your command line should really be,

"puts $stdin.readlines.sort_by {rand} unless not $stdin.eof"

and NOT

"puts $stdin.readlines.sort_by {rand} unless $stdin.eof".

This will enable you to put many lines because your $_ has
a value of EOF(nil) and we are saying "not $stdin.eof".
$_ contains the last entered line when
you do "gets" or "readline".

Hope this helps.

It didn't

I can replicate my procedure any number of times.

so, something your doing which is not right.

O Plameras