Reflecting on methods


#1

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

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]


#3

Yeah, that’s it !!!
Guess I was blind…

Thanks a lot !

Dominik

2007/6/22, removed_email_address@domain.invalid removed_email_address@domain.invalid: