# Getting existing context when defining a singleton method (class \<\< obj)

**URL:** <https://rubytalk.org/t/getting-existing-context-when-defining-a-singleton-method-class-obj/34269>\
**Category:** ruby-talk\
**Created:** [6 January 2007 16:15 UTC](https://rubytalk.org/t/getting-existing-context-when-defining-a-singleton-method-class-obj/34269 "2007-01-06T16:15:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roland\_Swingler](https://avatars.discourse-cdn.com/v4/letter/r/a87d85/32.png) [@Roland\_Swingler](https://rubytalk.org/u/Roland_Swingler)\
**Post date:** [6 January 2007 16:15 UTC](https://rubytalk.org/t/getting-existing-context-when-defining-a-singleton-method-class-obj/34269/1 "2007-01-06T16:15:14Z")

</div>

Hi

I'm fairly new to this "meta-programming" thing, so excuse me if I get  
my terminology wrong. Is there any way of getting access to the  
exisiting context when using class \<\< obj or otherwise creating a  
singleton method?

For example of what I mean - I have a method that returns a Proc:

def foo()  
&nbsp;&nbsp;local\_var = "Bar"  
&nbsp;&nbsp;Proc.new { local\_var }  
end

x = foo()  
x.call --\> returns "Bar", 'remembering' the local context

and I want to something similar with methods defined using class \<\<  
obj:

class MyObject  
end

def foo(my\_object\_instance)  
&nbsp;&nbsp;local\_var = "Bar"  
&nbsp;&nbsp;class \<\< my\_object\_instance  
&nbsp;&nbsp;&nbsp;&nbsp;define\_method(:a\_method) { local\_var }  
&nbsp;&nbsp;end  
end

o = MyObject.new  
foo(o)  
o.a\_method --\> throws an error, complaining about missing local  
variable

If I just wanted to assign this method to the class then I think I  
could do the following in foo:

my\_object\_instance.class.send(:define\_method, :a\_method) { local\_var }

But I want to define a singleton method, and I don't think you can get  
access to an object instance's virtual class in a similar way?

Any help with this much appreciated,

TIA  
Roland

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [6 January 2007 16:23 UTC](https://rubytalk.org/t/getting-existing-context-when-defining-a-singleton-method-class-obj/34269/2 "2007-01-06T16:23:23Z")

</div>

Hi --

> Hi
> 
> I'm fairly new to this "meta-programming" thing, so excuse me if I get  
> my terminology wrong. Is there any way of getting access to the  
> exisiting context when using class \<\< obj or otherwise creating a  
> singleton method?

Yes:

&nbsp;&nbsp;&nbsp;sclass = (class \<\< obj; self; end)  
&nbsp;&nbsp;&nbsp;sclass.class\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method ...  
&nbsp;&nbsp;&nbsp;end

That will keep you in the local scope you started in.

Hopefully there will be a method in Ruby 1.9/2.0 that will let you  
grab an object's singleton class, so you could do:

&nbsp;&nbsp;&nbsp;obj.singleton\_class.class\_eval # etc.

David

> **···**
>
> On Sun, 7 Jan 2007, Roland Swingler wrote:
> 
> --  
> Q. What is THE Ruby book for Rails developers?  
> A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> &nbsp;&nbsp;&nbsp;&nbsp;(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf\](http://www.rubypal.com/r4rrevs.pdf%5C))  
> Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
> A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Roland\_Swingler](https://avatars.discourse-cdn.com/v4/letter/r/a87d85/32.png) [@Roland\_Swingler](https://rubytalk.org/u/Roland_Swingler)\
**Post date:** [6 January 2007 16:45 UTC](https://rubytalk.org/t/getting-existing-context-when-defining-a-singleton-method-class-obj/34269/3 "2007-01-06T16:45:11Z")

</div>

Great!

> Yes:

Thanks for the (very speedy) response.

> Hopefully there will be a method in Ruby 1.9/2.0 that will let you  
> grab an object's singleton class, so you could do:
> 
> &nbsp;&nbsp;&nbsp;obj.singleton\_class.class\_eval # etc.

I guess you can have this in ruby 1.8 by doing

class Object  
&nbsp;&nbsp;def singleton\_class  
&nbsp;&nbsp;&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

Some code that I've seen around is now much clearer... thanks again.

Cheers,  
Roland

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [6 January 2007 16:50 UTC](https://rubytalk.org/t/getting-existing-context-when-defining-a-singleton-method-class-obj/34269/4 "2007-01-06T16:50:02Z")

</div>

Hi --

> Great!
> 
> > Yes:
> 
> Thanks for the (very speedy) response.
> 
> > Hopefully there will be a method in Ruby 1.9/2.0 that will let you  
> > grab an object's singleton class, so you could do:
> > 
> > &nbsp;&nbsp;&nbsp;obj.singleton\_class.class\_eval # etc.
> 
> I guess you can have this in ruby 1.8 by doing
> 
> class Object  
> def singleton\_class  
> &nbsp;&nbsp;&nbsp;class \<\< self  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self  
> &nbsp;&nbsp;&nbsp;end  
> end  
> end

Yes, many of us have had occasion to write exactly that 🙂

> Some code that I've seen around is now much clearer... thanks again.

Glad to help!

David

> **···**
>
> On Sun, 7 Jan 2007, Roland Swingler wrote:
> 
> --  
> Q. What is THE Ruby book for Rails developers?  
> A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> &nbsp;&nbsp;&nbsp;&nbsp;(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf\](http://www.rubypal.com/r4rrevs.pdf%5C))  
> Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
> A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
