Something in Ruby that bit me at work today
The methods :class and :dup are a bit of a leaky abstraction when used in combination with an object’s
Singleton Class:
class Marx
def jokes (laughs)
laughs * 2
end
end
groucho = Marx.new
chico = groucho.dup
puts "#{groucho.jokes(10)} == #{chico.jokes(10)}"
def groucho.jokes (laughs)
laughs / 2
end
harpo = groucho.dup
puts "#{groucho.jokes(10)} != #{harpo.jokes(10)}"
puts "...but #{harpo.jokes(10)} == #{chico.jokes(10)}"
puts "because Groucho is still considered a #{groucho.class}"