This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
Original Post: Shortening Perl with Ruby
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
As I'm working on porting the 43 page Worksheet.pm Perl module (and that's in a 10pt font), I've been able to cut down much of the overall length of the code by leveraging (eww) the power of Ruby.
Here's what I would consider a typical snippet of Perl code from Worksheet.pm:
sub center_horizontally {
my $self = shift;
if (defined $_[0]) {
$self->{_hcenter} = $_[0];
}
else {
$self->{_hcenter} = 1;
}
}
And here's the Ruby translation:
def center_horizontally(val = 1)
@hcenter = val
end
The ability to assign default values in an argument list, plus an implicit self generally means I can greatly reduce line (and character) count, yet retain readability. I can also eliminate all of the getters and setters with a simple attr_accessor call.