Home
shelling edited this page Sep 13, 2010
·
9 revisions
Rubyish Perl is targeting to be fun.
At this moment, you can define object methods with “def” in your class. Here we define a Cat class that’s a subclass of Animal. While
all animal can speak according to their sound.
class Animal {
attr_accessor "sound", "name", "weight"; # rubyish accessor builder
def speak {
print "A " . $self->class . " goes " . $self->sound
};
}
class Cat {
use base 'Animal'; # Still perlish Inheritance
def sound { "meow, meow" };
def hello($person) { # rubyish function prototype
"hello, $person, I am " . $self->name . "."; # auto-injected $self
};
}
$oreo = Cat->new->name("oreo");
$oreo->speak; #=> A Cat goes meow, meow
$oreo->hello("shelling"); #=> hello, shelling. I am oreo.
As you can see, a variable $self
is injected in to the body of methods.