Forum: Ruby Reflecting on methods

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
unknown (Guest)
on 2007-06-21 21:11
(Received via mailing list)
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
unknown (Guest)
on 2007-06-22 03:01
(Received via mailing list)
> 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]
unknown (Guest)
on 2007-06-22 05:24
(Received via mailing list)
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>:
This topic is locked and can not be replied to.