From: Robert Klemme Date: 2008-08-28T21:51:29+09:00 Subject: Re: How would this Perl script look in Ruby? 2008/8/28 Avinash Magar : > Nathan Powell wrote: >> Why don't you show us what you have, and where you are stuck, rather >> than us translating it for you in it's entirety. >> > sorry about that. > things I would like to know are: > > 1) How would we write following method? > the one which takes an array and return subref (whatever equivalent > in Ruby) > something like this(in perl) > sub get_random_val_generator { > my $arr = shift; > return sub { return $arr->[ rand @$arr] }; > } You can do array = ... rand_gen = lambda { array[rand(array.size)] } ... item = rand_gen[] # or item = rand_gen.call > 2) How to store these references(subrefs) in a hash or some variable? hash[some_key] = some_object # everything is an object > 3) How would you do(approach) this in Ruby(the script)? My Perl is a bit rusty and, frankly, I'm not inclined to spent the efforts to analyze it. Can you describe it? Cheers robert -- use.inject do |as, often| as.you_can - without end