Sha256: f45aca59a0feca4efb0e916ce70b2457e9aea0b24b18ea5f3bce33f6e3dfcdfd

Contents?: true

Size: 1.15 KB

Versions: 73

Compression:

Stored size: 1.15 KB

Contents

package Kindergarten;

my @default_students = qw(
   alice bob charlie david
   eve fred ginny harriet
   ileana joseph kincaid larry
);
my @students;

sub new {
    my ($class, $garden, $students) = @_;

    my $self = bless { garden => $garden } => $class;
    @students = $students ? map { lc } sort @$students : @default_students;
    
    return $self;
}

sub _map_garden_students {
    my $self = shift;
    my ($row_top, $row_bot) = split '\n' => $self->{garden};
    my ($offset, $width) = (0, 2);

    foreach my $student (@students) {
        $self->{table}->{$student} .= substr $row_top, $offset, $width;
        $self->{table}->{$student} .= substr $row_bot, $offset, $width;
        $offset += $width;
    }
}

sub _translate {
    my ($self, $student) = @_;

    $self->_map_garden_students;

    my %plants = qw(
        R radishes
        V violets
        C clover
        G grass
    );
    [ map { $plants{$_} } split '' => $self->{table}->{$student} ]
}

sub AUTOLOAD {
    my $sub = our $AUTOLOAD;
    $sub =~ s/.*:://;

    if ( grep { $_ eq $sub } @students ) {
        my $self = shift;
        return $self->_translate($sub);
    }
}

__PACKAGE__;

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm
trackler-2.2.1.179 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm
trackler-2.2.1.178 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm
trackler-2.2.1.177 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm
trackler-2.2.1.176 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm
trackler-2.2.1.175 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm
trackler-2.2.1.174 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm
trackler-2.2.1.173 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm
trackler-2.2.1.172 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm
trackler-2.2.1.171 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm
trackler-2.2.1.170 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm
trackler-2.2.1.169 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm
trackler-2.2.1.167 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm
trackler-2.2.1.166 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm
trackler-2.2.1.165 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm
trackler-2.2.1.164 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm
trackler-2.2.1.163 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm
trackler-2.2.1.162 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm
trackler-2.2.1.161 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm
trackler-2.2.1.160 tracks/perl5/exercises/kindergarten-garden/.meta/solutions/Kindergarten.pm