# Injecting an instance method into another class' class method

**URL:** <https://www.ruby-forum.com/t/injecting-an-instance-method-into-another-class-class-method/193123>\
**Category:** Ruby\
**Created:** [August 11, 2010, 5:33pm UTC](https://www.ruby-forum.com/t/injecting-an-instance-method-into-another-class-class-method/193123 "2010-08-11T17:33:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cremes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@cremes](https://www.ruby-forum.com/u/cremes)\
**Post date:** [August 11, 2010, 5:33pm UTC](https://www.ruby-forum.com/t/injecting-an-instance-method-into-another-class-class-method/193123/1 "2010-08-11T17:33:32Z")

</div>

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@:41(irb)\>  
ruby-1.9.1-p378 \> Foo.now  
=\> “bar now called”

Is it possible to do this?

cr

---

<div class="post-metadata">

**Author:** ![cremes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@cremes](https://www.ruby-forum.com/u/cremes)\
**Post date:** [August 12, 2010, 8:18pm UTC](https://www.ruby-forum.com/t/injecting-an-instance-method-into-another-class-class-method/193123/2 "2010-08-12T20:18:43Z")

</div>

On Aug 11, 2010, at 11:11 AM, JesÃºs Gabriel y GalÃ¡n wrote:

> > =\> 1281540640  
> > “Foo’s now”  
> > end
> 
> $ ruby redefine.rb  
> Foo’s now  
> Bar’s now  
> Bar’s now
> 
> The problem with an instance variable is that you need to have self as  
> Bar’s instance in order for it to work. So if you can use a local  
> variable and use closures, it will work, as shown above.

Ah, excellent! I was trying to figure out how to use a closure for this  
purpose, but my mind got all turned inside out.

I’m somewhat familiar with getting the singleton class of a class using  
the “class \<\< self;self;end” syntax, but I’m not sure what is happening  
here:

class \<\< Foo  
self # what is self here? Foo? Bar? instance of Bar?  
end

My inline questions lay it out.

I’d really like to understand how that part of it is working. I  
understand the mechanics of the remaining code.

cr

---

<div class="post-metadata">

**Author:** ![cremes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@cremes](https://www.ruby-forum.com/u/cremes)\
**Post date:** [August 11, 2010, 6:13pm UTC](https://www.ruby-forum.com/t/injecting-an-instance-method-into-another-class-class-method/193123/3 "2010-08-11T18:13:56Z")

</div>

On Wed, Aug 11, 2010 at 5:32 PM, Chuck R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> class Bar  
> end
> 
> cr

I’ve managed to do it with a local variable, instead of an instance  
variable:

class Foo  
def self.now  
“Foo’s now”  
end  
end

class Bar  
def initialize  
bar = proc {now}  
class \<\< Foo; self; end.class\_eval do  
define\_method(:now) do  
bar.call  
end  
end  
end

def now  
“Bar’s now”  
end  
end

puts Foo.now  
puts Bar.new.now  
puts Foo.now

$ ruby redefine.rb  
Foo’s now  
Bar’s now  
Bar’s now

The problem with an instance variable is that you need to have self as  
Bar’s instance in order for it to work. So if you can use a local  
variable and use closures, it will work, as shown above.

Jesus.

---

<div class="post-metadata">

**Author:** ![cremes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@cremes](https://www.ruby-forum.com/u/cremes)\
**Post date:** [August 12, 2010, 9:16pm UTC](https://www.ruby-forum.com/t/injecting-an-instance-method-into-another-class-class-method/193123/4 "2010-08-12T21:16:06Z")

</div>

Hi,

On Thu, Aug 12, 2010 at 11:48 PM, Chuck R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I’m somewhat familiar with getting the singleton class of a class using the “class \<\< self;self;end” syntax, but I’m not sure what is \>happening here:
> 
> class \<\< Foo  
> self # what is self here? Foo? Bar? instance of Bar?  
> end

since classes are objects too , for “class \<\< Foo; self; end” self is  
singleton (or eigenclass) class of the class object. Typically class  
methods of a class are defined in its singleton class.

[http://yugui.jp/articles/846](http://yugui.jp/articles/846)

–  
Let them talk of their oriental summer climes of everlasting  
conservatories; give me the privilege of making my own summer with my  
own coals.

[http://gnufied.org](http://gnufied.org)
