Wednesday, March 7, 2007

Ruby - Self

The "self" method is omitted from the Ruby API documentation. I was able to dig this explanation from one of my Ruby books:

"self" is a reference to the current object instance.
here is an example:

class Leaf
def print_self
p self
end
def to_s
"I am a leaf"
end
end

l = Leaf.new
l.to_s
=> "I am a leaf"
l.print_self
=> I am a leaf