C
Chuck Remes
I'd like to reopen a class Foo and redefine one of its methods to use an instance method from class Bar. I can't seem to figure out how to do it or even if it is possible.
I tried lots of different techniques and none have worked, so here I am asking for a bit of help. An example of one of the techniques I tried is included below.
class Foo
def self.now
Time.now.to_i
end
end
class Bar
def initialize
@bar = proc { now }
Bar.class_eval <<-code
class Foo
def Foo.now
@bar.call
end
end
code
end
def now
puts "bar now called"
end
end
I would like Foo.now to return Time.now.to_i for the normal case. However, after an instance of Bar is created, I would like the Bar instance to redefine Foo.now to call the #now instance method of Bar.
This is what the output should look like:
ruby-1.9.1-p378 > Foo.now
=> 1281540640
ruby-1.9.1-p378 > Bar.new
bar now
=> #<Bar:0x00000101216268 @bar=#<Proc:0x000001012161c0@(irb):41>>
ruby-1.9.1-p378 > Foo.now
=> "bar now called"
Is it possible to do this?
cr
I tried lots of different techniques and none have worked, so here I am asking for a bit of help. An example of one of the techniques I tried is included below.
class Foo
def self.now
Time.now.to_i
end
end
class Bar
def initialize
@bar = proc { now }
Bar.class_eval <<-code
class Foo
def Foo.now
@bar.call
end
end
code
end
def now
puts "bar now called"
end
end
I would like Foo.now to return Time.now.to_i for the normal case. However, after an instance of Bar is created, I would like the Bar instance to redefine Foo.now to call the #now instance method of Bar.
This is what the output should look like:
ruby-1.9.1-p378 > Foo.now
=> 1281540640
ruby-1.9.1-p378 > Bar.new
bar now
=> #<Bar:0x00000101216268 @bar=#<Proc:0x000001012161c0@(irb):41>>
ruby-1.9.1-p378 > Foo.now
=> "bar now called"
Is it possible to do this?
cr