Define_method with singleton classes?

Hi!

I'd like to use an existing proc object as a method for a singleton class.

q = proc { ... }
def foo.bar(*a)
q[*a]
end

doesn't work, of course, as q is a local var (and I wouldn't like to
pollute the environment with globals...)

So what springs to my mind is sending a :define_method to the singleton
class... I just can't access that class:

class << foo
end
foo.send(:define_method,:bar,q)
=> NoMethodError: undefined method `define_method' for #<Object:0x401a2614>

That is, the singleton class is created, but the value of the variable foo
(be it anything at this point) doesn't behave as a class, but as the
original foo.

Any ideas, hints, pointers?

Thanks,

···

--
Csaba

"There's more to life, than books, you know but not much more..."
[The Smiths]
***
If you want to send me a mail, see it in the mailto link at
http://www.renyi.hu/~ekho/egyelore.html

Hi,

At Tue, 15 Jun 2004 18:48:34 +0900,
Csaba Henk wrote in [ruby-talk:103654]:

class << foo
end

This singleton class has no effect.

foo.send(:define_method,:bar,q)
=> NoMethodError: undefined method `define_method' for #<Object:0x401a2614>

That is, the singleton class is created, but the value of the variable foo
(be it anything at this point) doesn't behave as a class, but as the
original foo.

Yes, foo itself doesn't change. You need to call define_method
in the singleton class context.

  q = proc {:q}
  foo = Object.new
  class << foo; self; end.module_eval do
    define_method(:bar,q)
  end
  p foo.bar # => :q

···

--
Nobu Nakada

Nobu wrote:

  class << foo; self; end.module_eval do

Csaba Henk - please vote here:
http://rcrchive.net/rcr/RCR/RCR231

for:

foo.singleton_class.class_eval do
#---^

daz

Nobu's vote, as well, could possibly be worth
about 60+ "normal" votes. :sunglasses:

Thanks, cool!

···

In article <200406151038.i5FAc5bk010477@sharui.nakada.niregi.kanuma.tochigi.jp>, nobu.nokada@softhome.net wrote:

  q = proc {:q}
  foo = Object.new
  class << foo; self; end.module_eval do
    define_method(:bar,q)
  end
  p foo.bar # => :q

--
Csaba

"There's more to life, than books, you know but not much more..."
[The Smiths]
***
If you want to send me a mail, see it in the mailto link at
http://www.renyi.hu/~ekho/egyelore.html

daz wrote:

Nobu wrote:

class << foo; self; end.module_eval do

Csaba Henk - please vote here:
http://rcrchive.net/rcr/RCR/RCR231

for:

foo.singleton_class.class_eval do
#---^

daz

Nobu's vote, as well, could possibly be worth
about 60+ "normal" votes. :sunglasses:

Yes, thank you, that is one of my favorite RCRs
also. (Thanks David Alan Black.)

Hal

Fine, I did so :slight_smile:

···

In article <OJWdnV3xJZ1i81LdSa8jmw@karoo.co.uk>, daz wrote:

Csaba Henk - please vote here:
http://rcrchive.net/rcr/RCR/RCR231

for:

foo.singleton_class.class_eval do
#---^

--
Csaba

"There's more to life, than books, you know but not much more..."
[The Smiths]
***
If you want to send me a mail, see it in the mailto link at
http://www.renyi.hu/~ekho/egyelore.html