# Function discovery

**URL:** <https://www.ruby-forum.com/t/function-discovery/155816>\
**Category:** Ruby\
**Created:** [December 17, 2008, 6:45am UTC](https://www.ruby-forum.com/t/function-discovery/155816 "2008-12-17T06:45:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jballanc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jballanc](https://www.ruby-forum.com/u/jballanc)\
**Post date:** [December 17, 2008, 6:45am UTC](https://www.ruby-forum.com/t/function-discovery/155816/1 "2008-12-17T06:45:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jballanc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jballanc](https://www.ruby-forum.com/u/jballanc)\
**Post date:** [December 17, 2008, 12:47pm UTC](https://www.ruby-forum.com/t/function-discovery/155816/2 "2008-12-17T12:47:28Z")

</div>

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/)

---

<div class="post-metadata">

**Author:** ![jballanc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jballanc](https://www.ruby-forum.com/u/jballanc)\
**Post date:** [December 17, 2008, 8:15pm UTC](https://www.ruby-forum.com/t/function-discovery/155816/3 "2008-12-17T20:15:09Z")

</div>

> [Note: parts of this message were removed to make it a legal post.]

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

Joshua B. 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!

---

<div class="post-metadata">

**Author:** ![jballanc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jballanc](https://www.ruby-forum.com/u/jballanc)\
**Post date:** [December 17, 2008, 7:12am UTC](https://www.ruby-forum.com/t/function-discovery/155816/4 "2008-12-17T07:12:04Z")

</div>

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 B. wrote:

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

einarmagnus

---

<div class="post-metadata">

**Author:** ![jballanc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jballanc](https://www.ruby-forum.com/u/jballanc)\
**Post date:** [December 17, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/function-discovery/155816/5 "2008-12-17T20:16:56Z")

</div>

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
