[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Reflecting on methods

cypher.dp

6/21/2007 5:10:00 PM

Hi guys !

I'm a little bit confused about the right way to get the methods of a
class/module.

For this class

class Test
public
def Test.pub_class_meth; end

protected
def Test.prot_class_meth; end

private
def Test.priv_class_meth; end

public
def pub_meth; end

protected
def prot_meth; end

private
def priv_meth; end
end

arr = []
Object.methods.each {|m| arr << m if m =~ /methods/}
arr.each {|m| puts m + " = [" + (Test.send(m) -
Object.send(m)).join(", ") + "]"}

I get the following result:

methods = [priv_class_meth, pub_class_meth, prot_class_meth]
private_instance_methods = [priv_meth]
singleton_methods = [priv_class_meth, prot_class_meth, pub_class_meth]
instance_methods = [pub_meth, prot_meth]
protected_methods = []
private_methods = []
public_instance_methods = [pub_meth]
protected_instance_methods = [prot_meth]
public_methods = [priv_class_meth, pub_class_meth, prot_class_meth]

Question: Why is "prot_meth" not listed in protected_methods ? And
"priv_meth" in private_methods ?
In general: Which of these *_methods gives which result ?

Thanks in advance

Dominik

2 Answers

rking

6/21/2007 10:58:00 PM

0

> Question: Why is "prot_meth" not listed in protected_methods ? And "priv_meth" in private_methods ?
> In general: Which of these *_methods gives which result ?

You are calling .protected_methods and .private_methods on the Test
class object, not a Test instance object.

This code:
%w(protected_methods private_methods).each {|m| puts m + " = [" +
(Test.new.send(m) - Object.new.send(m)).join(',') + "]" }
gives:
protected_methods = [prot_meth]
private_methods = [priv_meth]

cypher.dp

6/22/2007 1:23:00 AM

0

Yeah, that's it !!!
Guess I was blind...

Thanks a lot !

Dominik


2007/6/22, rking@panoptic.com <rking@panoptic.com>:
> > Question: Why is "prot_meth" not listed in protected_methods ? And "priv_meth" in private_methods ?
> > In general: Which of these *_methods gives which result ?
>
> You are calling .protected_methods and .private_methods on the Test
> class object, not a Test instance object.
>
> This code:
> %w(protected_methods private_methods).each {|m| puts m + " = [" +
> (Test.new.send(m) - Object.new.send(m)).join(',') + "]" }
> gives:
> protected_methods = [prot_meth]
> private_methods = [priv_meth]
>
>
>