[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: define_method confusery

Arnaud Bergeron

9/28/2006 4:15:00 PM

On 9/28/06, Martin Coxall <pseudo.meta@gmail.com> wrote:
[snip]
>
> # List the methods of Klass
> Klass.methods.sort.each do |method|

Try Klass.instance_methods.

> puts "Klass##{methodName}"
> end
>
> ----------------------------------------------------------------

2 Answers

Rick DeNatale

9/28/2006 9:31:00 PM

0

On 9/28/06, Martin Coxall <pseudo.meta@gmail.com> wrote:
> On 9/28/06, Arnaud Bergeron <abergeron@gmail.com> wrote:
> >
> > On 9/28/06, Martin Coxall <pseudo.meta@gmail.com> wrote:
> > [snip]
> > >
> > > # List the methods of Klass
> > > Klass.methods.sort.each do |method|
> >
> > Try Klass.instance_methods.
>
>
> Aha! That would do it. Thanks.
>
> Do you know if it's possible to add class methods to classes using
> define_method() then? It seems to work if I do a
>
> define_method("Klass."+methodName)
>
> However, when I then try to invoke using
>
> Klass.send(methodName, "some stuff")
>
> It doesn't work.

You need to send define_method to the classes singleton class, and
that takes a trick:

rick@frodo:/public/rubyscripts$ cat def_class_meth.rb
#! /usr/bin/ruby
#

def create_method(name, klazz, meth = nil, &b)
raise ArgumentError "give method or block, but not both" if meth &&
block_given?
if block_given?
klazz.send(:define_method, name, &b)
else
klazz.send(:define_method, name, meth)
end
end

def create_class_method(name, klazz, meth=nil, &b)
klazz_klazz = class << klazz; self; end
create_method(name, klazz_klazz, meth, &b)
end

class Foo
end

create_method(:foo_inst_meth, Foo) {"This is an instance method of Foo"}
create_class_method(:foo_class_meth, Foo) {"This is a class method of Foo"}

puts Foo.new.foo_inst_meth
puts Foo.foo_class_meth


rick@frodo:/public/rubyscripts$ ruby def_class_meth.rb
def_class_meth.rb:5: warning: parenthesize argument(s) for future version
This is an instance method of Foo
This is a class method of Foo
rick@frodo:/public/rubyscripts$

Note that in ruby 1.9 send won't call a private method anymore, you
need to use funcall instead.
http://eigenclass.org/hiki.rb?Changes+in+Ru...

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Martin Coxall

9/28/2006 10:36:00 PM

0

> You need to send define_method to the classes singleton class, and
> that takes a trick:
>

Okay, I think I understand why that is, but I'd better re-read
Programming Ruby on Singleton classes.

>
> def create_class_method(name, klazz, meth=nil, &b)
> klazz_klazz = class << klazz; self; end
> create_method(name, klazz_klazz, meth, &b)
> end

Okay, I can kind of see that. I think. Um.

Thanks very much.

Martin