How would this Perl script look in Ruby?

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;

;--------------------------------------------------

thanks in advance.

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.

On Thu, Aug 28, 2008 at 09:23:30PM +0900, Avinash M. wrote:

'adm has _QTY_ _CAR_ cars',
    'order' => 'rand',
    my ($nval) = $v->{gen}->();
return sub {

;--------------------------------------------------

thanks in advance.

Posted via http://www.ruby-forum.com/.


nathan
nathan_at_nathanpowell_dot_org

At this point, anyone proposing to run Windows on servers should be
prepared
to explain what they know about servers that Google, Yahoo, and Amazon
don’t.
~ Paul Graham

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] };
    }

  2. How to store these references(subrefs) in a hash or some variable?

  3. How would you do(approach) this in Ruby(the script)?

Since I know very little about Ruby so I don’t the construct used for
this purpose?

2008/8/28 Avinash M. [email protected]:

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

  1. How to store these references(subrefs) in a hash or some variable?

hash[some_key] = some_object # everything is an object

  1. 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

Avinash M. wrote:

Can somebody please me tell the way to write this script in Ruby?

just do this - no-one will ever know :wink:

system “perl #{ARGV[0]}”

On Thu, Aug 28, 2008 at 2:51 PM, Robert K.
[email protected]wrote:

in Ruby)

efforts to analyze it. Can you describe it?
Unless I’m mistaken, the gist of it is:

Take a bunch of templates:

adm has QTY CAR cars
ssn has QTY CAR cars

In an infinite loop, pick one randomly, and replace the QTY and CAR
placeholders by values taken from the ‘vals’ fields in the following
hash:

my %value_map = (
CAR’ => {
‘order’ => ‘rand’,
‘vals’ => [qw(bentley merc bmw audi porsche ford honda lexus)]
},
QTY’ => {
‘order’ => ‘rand’,
‘vals’ => [1…10]
}
);

The way values are chosen depends on the ‘order’ field, which can be
either
random (‘rand’) or sequential (‘seq’).

In ruby, you could replace the value_map by using a more class-oriented
approach:

RandomMessageValue.new(:qty, (1…10).to_a)
RandomMessageValue.new(:car, %w(bentley merc bmw audi porsche ford honda
lexus)

And you have SeqMessageValue if you want something sequential.
Both RandomMessageValue and SeqMessageValue inherit from MessageValue.
A MessageValue object knows how to choose the next value in the array of
values (with different implementations). Then you just need a Message
class,
which has a list of templates, and a list of MessageValue objects.
Message
knows how to replace the content of a MessageValue inside a template.

Voilà .

Cheers,

Emm

On Aug 28, 7:23 am, Avinash M. [email protected] wrote:

'adm has _QTY_ _CAR_ cars',
    'order' => 'rand',
    my ($nval) = $v->{gen}->();
my $last = 0;
return sub { return $arr->[ rand @$arr] };
    die "Invalid value order in value_map\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

On Sep 2, 11:20 pm, William J. [email protected].invalid wrote:

use Data::Dumper;
‘order’ => ‘rand’,
my ($next_msg, $val_map) = @_;

return $arrya[$last + 1] elem of array; $last is remembered across

    $v->{gen} = get_seq_val_generator($v->{vals});

message_templates =
QTY’ => [ get_rand, (1…10).to_a ]
ssn had 1 honda 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs