S
Simon Strandgaard
'obj.extend' and 'class<<obj' are close cousins..
However there is a difference.. 'class<<obj' doesn't
invoke #extend_object, and #extended. To me it feels
inconsistent that there is no way to execute code on the
point when the extension occurs.
I propose that 'class<<obj' invokes #extend_object and #extended.
Lets make a comparison.. first I present some code
which works using #extend.
Next I present some code which doesn't work.. but
which should work.
server> ruby a.rb
hello
server> cat a.rb
module M
def self.extended(parent)
parent.init_m
end
def init_m
@str = "hello"
end
def hello
puts @str
end
end
class C
end
c = C.new
c.extend(M)
c.hello
server>
The next part doesn't work.. (thus the proposal)
server> cat b.rb
class C
end
c = C.new
class << c
def self.extended(parent) # proposal: invoke me ;-)
parent.init_m
end
def init_m
@str = "hello"
end
def hello
puts @str
end
end
c.hello
server> ruby b.rb
b.rb:13: warning: instance variable @str not initialized
nil
server>
However there is a difference.. 'class<<obj' doesn't
invoke #extend_object, and #extended. To me it feels
inconsistent that there is no way to execute code on the
point when the extension occurs.
I propose that 'class<<obj' invokes #extend_object and #extended.
Lets make a comparison.. first I present some code
which works using #extend.
Next I present some code which doesn't work.. but
which should work.
server> ruby a.rb
hello
server> cat a.rb
module M
def self.extended(parent)
parent.init_m
end
def init_m
@str = "hello"
end
def hello
puts @str
end
end
class C
end
c = C.new
c.extend(M)
c.hello
server>
The next part doesn't work.. (thus the proposal)
server> cat b.rb
class C
end
c = C.new
class << c
def self.extended(parent) # proposal: invoke me ;-)
parent.init_m
end
def init_m
@str = "hello"
end
def hello
puts @str
end
end
c.hello
server> ruby b.rb
b.rb:13: warning: instance variable @str not initialized
nil
server>