On Sep 2, 11:20 pm, William James <w_a_x_... / yahoo.com> wrote:
> On Aug 28, 7:23 am, Avinash Magar <avinash.ma... / gmail.com> wrote:
>
>
>
> > Can somebody please me tell the way to write this script in Ruby?
>
> > ;---------------------------------------
> > #!/bin/env perl
>
> > use strict;
> > use Data::Dumper;
>
> > #; define some message templates here
> > my $message_templates = [
> >     'adm has _QTY_ _CAR_ cars',
> >     'ssn had _QTY_ _CAR_ cars',
> > ];
>
> > #; value map
> > my %value_map = (
> >     '_CAR_' => {
> >         'order' => 'rand',
> >         'vals' => [qw(bentley merc bmw audi porsche ford honda lexus)]
> >         },
> >     '_QTY_' => {
> >         'order' => 'rand',
> >         'vals' => [1..10]
> >         }
> > );
>
> > # using 'random_msg_stub and val generators from %value_map
> > sub get_next_random_msg {
> >     my ($next_msg, $val_map) = @_;
>
> >     my $m = &$next_msg();
> >     while (my($vk, $v) = each (%value_map)) {
> >         my ($nval) = $v->{gen}->();
> >         $m =~ s/$vk/$nval/g;
> >     }
> >     $m;
>
> > }
>
> > # return $arrya[$last + 1] elem of array; $last is remembered across
> > invocations
> > sub get_seq_val_generator {
> >     my $a = $_[0];
> >     my $last = 0;
> >     return sub {
> >         $last = 0 if ($last == scalar @$a);
> >         $$a[$last++];
> >     };
>
> > }
>
> > # return random elem of array
> > sub get_random_val_generator {
> >     my $arr = shift;
> >     return sub { return $arr->[ rand @$arr] };
>
> > }
>
> > #; let's generate value generator foreach value in 'value_map
> > while (my($vk, $v) = each (%value_map)) {
> >     if ($v->{order} eq 'seq') {
> >         $v->{gen} = get_seq_val_generator($v->{vals});
> >     } elsif ($v->{order} eq 'rand') {
> >         $v->{gen} = get_random_val_generator($v->{vals});
> >     } else {
> >         die "Invalid value order in value_map\n";
> >     }
>
> > }
>
> > my ($get_next_random_msg_stub) =
> > get_random_val_generator($message_templates);
>
> > while (1) {
> >     print get_next_random_msg($get_next_random_msg_stub, \%value_map),
> > "\n\n";
>
> > }
>
> > 1;
>
> > ;--------------------------------------------------
>
> message_templates =
>   'adm has _QTY_ _CAR_ cars',
>   'ssn had _QTY_ _CAR_ cars'
>
> get_rand = proc{|a| a[ rand( a.size ) ] }
> $last = -1
> get_seq  = proc{|a| $last = $last.next % a.size; a[$last] }
>
> value_map = {
>   '_CAR_' => [ get_seq,
>       %w(bentley merc bmw audi porsche ford honda lexus) ],
>   '_QTY_' => [ get_rand, (1..10).to_a ]
>
> }
>
> while true do
>   puts get_rand[ message_templates ].gsub( /_.*?_/ ){|s|
>     prc, list = value_map[ s ]
>     prc[ list ] }
> end
>
> --- output ---
> ssn had 1 honda cars
> ssn had 10 lexus cars
> adm has 2 bentley cars
> adm has 6 merc cars
> adm has 4 bmw cars
> adm has 4 audi cars
> ssn had 7 porsche cars
> adm has 3 ford cars
> ssn had 5 honda cars
> adm has 10 lexus cars
> ssn had 7 bentley cars
> adm has 6 merc cars
> ...

message_templates =
  'adm has _QTY_ _CAR_ cars',
  'ssn had _QTY_ _CAR_ cars'

get_rand = proc{|a| a[ rand( a.size ) ] }
get_seq  = proc{|a| a.last[ a[0] = a[0].next % a.last.size ] }

value_map = {
  '_CAR_' => [ get_seq, [ -1,
      %w(bentley merc bmw audi porsche ford honda lexus) ] ],
  '_QTY_' => [ get_rand, (1..10).to_a ]
}

while true do
  puts get_rand[ message_templates ].gsub( /_.*?_/ ){|s|
    prc, list = value_map[ s ]
    prc[ list ] }
end