S
Satsou Sa
Hello,
I've got some troubles with Ruby about callbacks (and inheritance). Here
is my code:
class Lmao
def initialize
@str = "HAHAHAHAHAHHAHAHAH"
@before_laughing = []
end
def self.inherited(base)
base.extend(Callbacks)
end
def laughing
@before_laughing.each {|method| send(method) }
@str
end
end
module Callbacks
def before_laughing(*methods)
@before_laughing = methods
end
end
class Lol < Lmao
before_laughing :downcase_please
def downcase_please
@str.downcase!
end
end
a = Lol.new
a.laughing # => "HAHAHAHAHAHHAHAHAH"
And as you can see, my before laughing callback don't work... because
the array @before_laughing is empty. I believe this can be fixed by
editing the way I save *methods into an Lol's instance method (from
inside Callbacks). But I don't really see how...
If you know the solution, thanks for your light!
I've got some troubles with Ruby about callbacks (and inheritance). Here
is my code:
class Lmao
def initialize
@str = "HAHAHAHAHAHHAHAHAH"
@before_laughing = []
end
def self.inherited(base)
base.extend(Callbacks)
end
def laughing
@before_laughing.each {|method| send(method) }
@str
end
end
module Callbacks
def before_laughing(*methods)
@before_laughing = methods
end
end
class Lol < Lmao
before_laughing :downcase_please
def downcase_please
@str.downcase!
end
end
a = Lol.new
a.laughing # => "HAHAHAHAHAHHAHAHAH"
And as you can see, my before laughing callback don't work... because
the array @before_laughing is empty. I believe this can be fixed by
editing the way I save *methods into an Lol's instance method (from
inside Callbacks). But I don't really see how...
If you know the solution, thanks for your light!