# Getting existing context when defining a singleton method (c

**URL:** <https://www.ruby-forum.com/t/getting-existing-context-when-defining-a-singleton-method-c/81250>\
**Category:** Ruby\
**Created:** [January 6, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/getting-existing-context-when-defining-a-singleton-method-c/81250 "2007-01-06T19:30:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![schmitr3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea666f/32.png) [@schmitr3](https://www.ruby-forum.com/u/schmitr3)\
**Post date:** [January 6, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/getting-existing-context-when-defining-a-singleton-method-c/81250/1 "2007-01-06T19:30:42Z")

</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()  
local\_var = “Bar”  
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)  
local\_var = “Bar”  
class \<\< my\_object\_instance  
define\_method(:a\_method) { local\_var }  
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:** ![schmitr3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea666f/32.png) [@schmitr3](https://www.ruby-forum.com/u/schmitr3)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/getting-existing-context-when-defining-a-singleton-method-c/81250/2 "2007-01-19T16:30:50Z")

</div>

Hi –

On Sun, 7 Jan 2007, Roland S. wrote:

> 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:

sclass = (class \<\< obj; self; end)  
sclass.class\_eval do  
define\_method …  
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:

obj.singleton\_class.class\_eval # etc.

David

---

<div class="post-metadata">

**Author:** ![schmitr3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea666f/32.png) [@schmitr3](https://www.ruby-forum.com/u/schmitr3)\
**Post date:** [September 25, 2007, 10:37pm UTC](https://www.ruby-forum.com/t/getting-existing-context-when-defining-a-singleton-method-c/81250/3 "2007-09-25T22:37:46Z")

</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:
> 
> obj.singleton\_class.class\_eval # etc.

I guess you can have this in ruby 1.8 by doing

class Object  
def singleton\_class  
class \<\< self  
self  
end  
end  
end

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

Cheers,  
Roland

---

<div class="post-metadata">

**Author:** ![schmitr3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea666f/32.png) [@schmitr3](https://www.ruby-forum.com/u/schmitr3)\
**Post date:** [September 25, 2007, 10:52pm UTC](https://www.ruby-forum.com/t/getting-existing-context-when-defining-a-singleton-method-c/81250/4 "2007-09-25T22:52:19Z")

</div>

Hi –

On Sun, 7 Jan 2007, Roland S. wrote:

> I guess you can have this in ruby 1.8 by doing
> 
> class Object  
> def singleton\_class  
> class \<\< self  
> self  
> 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
