T
Tom Werner
I've got a question regarding the inner workings of extend. Consider the
following code that uses Object#extend twice:
class Klass
def hello(options = {})
p options
end
end
module Mod1
def hello(options = {})
super(options.merge({:mod1 => true}))
end
end
module Mod2
def hello(options = {})
super(options.merge({:mod2 => true}))
end
end
k = Klass.new
k.hello #=> {}
k.extend(Mod1)
k.hello #=> {:mod1=>true}
k.extend(Mod2)
k.hello #=> {:mod2=>true, :mod1=>true}
I was a bit surprised that the second extend didn't clobber the first.
How is this handled by the Ruby interpreter? Is it creating more than
one eigenclass?
Tom
--
Tom Werner
Helmets to Hardhats
Software Developer
(e-mail address removed)
www.helmetstohardhats.org
following code that uses Object#extend twice:
class Klass
def hello(options = {})
p options
end
end
module Mod1
def hello(options = {})
super(options.merge({:mod1 => true}))
end
end
module Mod2
def hello(options = {})
super(options.merge({:mod2 => true}))
end
end
k = Klass.new
k.hello #=> {}
k.extend(Mod1)
k.hello #=> {:mod1=>true}
k.extend(Mod2)
k.hello #=> {:mod2=>true, :mod1=>true}
I was a bit surprised that the second extend didn't clobber the first.
How is this handled by the Ruby interpreter? Is it creating more than
one eigenclass?
Tom
--
Tom Werner
Helmets to Hardhats
Software Developer
(e-mail address removed)
www.helmetstohardhats.org