[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

#methods and #public_instance_methods

Jeff

12/14/2005 5:24:00 PM

I'm trying to understand #methods and #public_instance_methods as I
learn about introspection in Ruby.

If I do String.methods in irb, I get a lot fewer listed than if I try
String.public_instance_methods. I would have guessed that
String.public_instance_methods would be a subset of String.methods.

Also, ri String.public_instance_methods has no information, and
public_instance_methods is not listed on ruby-doc.org/core. I only
found it by doing a String.methods.

Can anyone shed some light here?

Thanks
Jeff

--
Posted via http://www.ruby-....


3 Answers

Its Me

12/14/2005 5:54:00 PM

0

String.instance_methods == "abc".methods

String.instance_methods gives methods on instances of String.

String.methods gives methods on String itself.

Hth.


"Jeff Cohen" <cohen.jeff@gmail.com> wrote in message
news:bcd0453943d7bed44939379f19791061@ruby-forum.com...
> I'm trying to understand #methods and #public_instance_methods as I
> learn about introspection in Ruby.
>
> If I do String.methods in irb, I get a lot fewer listed than if I try
> String.public_instance_methods. I would have guessed that
> String.public_instance_methods would be a subset of String.methods.
>
> Also, ri String.public_instance_methods has no information, and
> public_instance_methods is not listed on ruby-doc.org/core. I only
> found it by doing a String.methods.
>
> Can anyone shed some light here?
>
> Thanks
> Jeff
>
> --
> Posted via http://www.ruby-....
>
>


Esteban Manchado Velázquez

12/14/2005 6:01:00 PM

0

Hi Jeff,

On Thu, Dec 15, 2005 at 02:24:10AM +0900, Jeff Cohen wrote:
> I'm trying to understand #methods and #public_instance_methods as I
> learn about introspection in Ruby.
>
> If I do String.methods in irb, I get a lot fewer listed than if I try
> String.public_instance_methods. I would have guessed that
> String.public_instance_methods would be a subset of String.methods.
>
> Also, ri String.public_instance_methods has no information, and
> public_instance_methods is not listed on ruby-doc.org/core. I only
> found it by doing a String.methods.
>
> Can anyone shed some light here?

#methods returns the list of methods callable for that _object_. That is,
if you call String.methods, you get the methods for the String object (the
object of class Class, if you prefer :-P ).

#public_instance_methods, on the other hand, is a method defined for Class
objects (perhaps some other, but anyway). It returns the methods callable for
the _objects of that class_.

E.g.: if String.methods return ['a', 'b', 'c'] and
String.public_instance_methods return ['d', 'e', 'f'], you can call String.a,
String.b, String.c, "foo".d, "bar".e, "qux".f.

You can also call String.public_instance_methods(false) if you only want
the methods defined in the class String, but not inherited from
String.superclass.

HTH,

--
Esteban Manchado Velázquez <zoso@foton.es> - http://ww...
EuropeSwPatentFree - http://EuropeSwPatentFree.his...

Jeff

12/14/2005 6:53:00 PM

0

Got it.

Thanks everyone, that helps a lot.

Jeff

--
Posted via http://www.ruby-....