On Thursday 15 June 2006 20:42, you wrote:
> On 6/15/06, Graham Smith <[EMAIL PROTECTED]> wrote:
> > Here are my solutions to the exercises
> >
> > #!/usr/bin/ruby
> > a=[]
> > 20.times { x = rand(100) + 1; a << x }
> > puts a.select { |v| v if v % 2 == 1 }
>
> nice
>
> > - Read all lines from standard input, then print them out in a random
> >
> > > order. (Hint: Array#sort_by)  (Extra hint: There's more to the IO class
> > > than each_line -- there are also methods to read lines)
> >
> > #!/usr/bin/ruby
> > a=STDIN.readlines.sort_by {rand}
> > puts a
>
> any reason why you chose STDIN

The reason I used STDIN was it complies with the exercise. Also for testing it 
is easy to pipe someting to the file to test with, as I hate typing. e.g if 
the above code is in a file called rand_array.rb you can do
ls -l ~ | rand_array.rb 
will give you a randomised listing of your home directory  

STDIN is the constant for the predefined variable $stdin although you could 
use $< but I believe it is not very descriptive and difficult to read in a 
whole lot of code, unless you know all the symbols and their meanings off by 
heart..

-- 
Regards,

Graham Smith
_______________________________________________
coders mailing list
[email protected]
http://lists.slug.org.au/listinfo/coders

Reply via email to