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

  • Thread starter Roland Swingler
  • Start date
R

Roland Swingler

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
 
D

dblack

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:

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

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
R

Roland Swingler

Great!

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
 
D

dblack

Hi --

Great!


Thanks for the (very speedy) response.


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

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top