function discovery

J

Joshua Ball

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

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
 
E

Einar Magnús Boson

f.methods
f.methods(false) to skip the inherited ones.
usually this is helpful:

puts f.methods(false).sort

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

einarmagnus
 
B

Brian Candler

Einar said:
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/)
 
P

Phlip

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

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

Joshua said:
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!
 
R

Robert Klemme

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

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

Joshua said:
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top