[ next ] [ prev ] [ contents ] Using Ruby

Singleton Methods

Singleton methods are per-object methods

class Dog
  def talk
    puts "Woof"
  end
end

fido  = Dog.new
rover = Dog.new

def fido.wag
 puts "I'm Happy!"
end

fido.talk   # => Woof
rover.talk  # => Woof

fido.wag    # => I'm Happy!
rover.wag   # => NoMethodError Exception


[ next ] [ prev ] [ contents ] Copyright 2003 by Jim Weirich.
Some Rights Reserved