How would this Perl script look in Ruby?

A

Avinash Magar

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

Nathan Powell

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.

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.

--
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
------------------------------------
 
A

Avinash Magar

Nathan said:
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?
 
R

Robert Klemme

2008/8/28 Avinash Magar said:
Nathan said:
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
 
E

Emmanuel Surleau

2008/8/28 Avinash Magar said:
Nathan said:
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 =3D shift;
return sub { return $arr->[ rand @$arr] };
}

You can do

array =3D ...
rand_gen =3D lambda { array[rand(array.size)] }
...
item =3D rand_gen[] # or
item =3D rand_gen.call
2) How to store these references(subrefs) in a hash or some variable?

hash[some_key] =3D 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?


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 =3D (
'_CAR_' =3D> {
'order' =3D> 'rand',
'vals' =3D> [qw(bentley merc bmw audi porsche ford honda lexus)]
},
'_QTY_' =3D> {
'order' =3D> 'rand',
'vals' =3D> [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=E0.

Cheers,

Emm
 
J

John Pritchard-williams

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

<joke>

just do this - no-one will ever know ;-)

system "perl #{ARGV[0]}"

</joke>
 
W

William James

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

William James

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";


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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top