Forum: Ruby function discovery

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.
B15131e5df68c72103eac27911680873?d=identicon&s=25 Joshua Ball (Guest)
on 2008-12-17 06:45
(Received via mailing list)
I am curious if there is a programatic way to list all methods of a
class.
(Yes, I know about "ri", I am just curious here)

For instance, I am trying to figure out what I could do with the File f
in:
open(ARGV[0], 'rb') { |f|
  puts f.stat.mtime
  puts f.class
}

Now, f.class returns: File

Is there anything like:
  f.functions.each { |p| puts p }

Thanks
87349a7a95b3f2e83c20194ef122885c?d=identicon&s=25 Einar Magnús Boson (Guest)
on 2008-12-17 07:12
(Received via mailing list)
f.methods
f.methods(false) to skip the inherited ones.
usually this is helpful:

puts f.methods(false).sort

On 17.12.2008, at 05:36 , Joshua Ball wrote:

>
> Now, f.class returns: File
>
> Is there anything like:
>  f.functions.each { |p| puts p }
>
> Thanks

einarmagnus
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-12-17 12:47
Einar Magnús Boson wrote:
> f.methods
> f.methods(false) to skip the inherited ones.
> usually this is helpful:
>
> puts f.methods(false).sort

Also useful:

Object.constants.grep(/RUBY/)
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2008-12-17 20:15
(Received via mailing list)
> [Note:  parts of this message were removed to make it a legal post.]

I express no curiosity about the illegal parts! (-;

Joshua Ball wrote:

> I am curious if there is a programatic way to list all methods of a class.
> (Yes, I know about "ri", I am just curious here)

ri only returns what the documentor felt like telling you.

Here's my favorite wrapper on the 'public_methods' system:

   def what?(anObject)
     p anObject.class.name
     puts (anObject.public_methods - Object.new.public_methods).sort
   end

Now you just say 'what? f', and it tells you what f's all about. It also
throws
away all the stuff f inherited from Object, because you should already
know it!
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-12-17 20:16
(Received via mailing list)
On 17.12.2008 18:24, Phlip wrote:
> ri only returns what the documentor felt like telling you.
>
> Here's my favorite wrapper on the 'public_methods' system:
>
>   def what?(anObject)
>     p anObject.class.name
>     puts (anObject.public_methods - Object.new.public_methods).sort
>   end

You do not need to construct an instance of Object - you can as well use
Object.public_instance_methods.

> Now you just say 'what? f', and it tells you what f's all about. It also
> throws away all the stuff f inherited from Object, because you should
> already know it!

Here's an alternative version, which prints methods with the defining
class.  Note that this omits methods defined at the instance level.

def what? o
   o.class.ancestors.each do |cl|
     p cl, cl.public_instance_methods(false).sort
   end
end

Kind regards

  robert
This topic is locked and can not be replied to.