list methods only for specific class

L

Le Sa

hello folks!

Is there a way to list methods only for specific class, i.e., not
inherited?

Thank you!
 
D

Daniel Roux

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

hello folks!

Is there a way to list methods only for specific class, i.e., not
inherited?

Thank you!

object.public_methods(false)

Hope this helps!
 
L

Le Sa

I would like to list ms1 and ms2, but thanks for the help!

class Person
def mp1() end
end

class Student < Person
def ms1() end
def ms2() end

private :ms2
end

Student.public_methods(false).each {|m|puts m}
puts "************\n"
Student.instance_methods(false).each{|m|puts m}
=begin
OUTPUT IS:

yaml_tag_subclasses?
allocate
to_yaml
superclass
new
************
ms1
=end
 
L

Le Sa

No problem!
instance_methods(false) is fine, because I won't be able to use private
methods anyway xD
 
M

Michael Kohl

I would like to list ms1 and ms2, but thanks for the help!

class Person
=A0def mp1() =A0end
end

class Student < Person
=A0def ms1() end
=A0def ms2() end

=A0private :ms2
end

The closest I have to offer is this:
=3D> ["ms1", "mp1"]
 
D

Daniel Roux

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

I would like to list ms1 and ms2, but thanks for the help!

class Person
def mp1() end
end

class Student < Person
def ms1() end
def ms2() end

private :ms2
end

Student.public_methods(false).each {|m|puts m}
puts "************\n"
Student.instance_methods(false).each{|m|puts m}
=begin
OUTPUT IS:

yaml_tag_subclasses?
allocate
to_yaml
superclass
new
************
ms1
=end
Try:
Student.new.public_methods(false) + Student.new.private_methods(false)
=> ["ms1", "ms2"]
 
R

Robert Klemme

2009/6/15 Daniel Roux said:
Try:
Student.new.public_methods(false) + Student.new.private_methods(false)
=> ["ms1", "ms2"]

Why do you folks create instances? You can do this as well:

cl = any_class
cl.instance_methods(false) - cl.private_instance_methods

Kind regards

robert
 
S

s.ross

2009/6/15 Daniel Roux said:
Try:
Student.new.public_methods(false) +
Student.new.private_methods(false)
=> ["ms1", "ms2"]

Why do you folks create instances? You can do this as well:

cl = any_class
cl.instance_methods(false) - cl.private_instance_methods

Kind regards

robert


Often, I'm most interested in methods unique to a given class and not
those inherited from, say, Object:

(Array.instance_methods - Array.private_instance_methods -
Object.public_methods).sort

And I sort them for easier reference. Don't know if this adds any
information...
 
J

Jonathan Rochkind

Steve said:
Often, I'm most interested in methods unique to a given class and not
those inherited from, say, Object:

(Array.instance_methods - Array.private_instance_methods -
Object.public_methods).sort

And I sort them for easier reference. Don't know if this adds any
information...

If you really want unique to the class, and the class may have a more
complex object hieararchy than just inheriting from Object, why not
something like:

(MyClass.methods - MyClass.superclass.methods)

I _think_ that'll work, although how included modules in MyClass are
treated should be tested experimentally. I think it'll still work cause
of the magic anonymous singleton class thing ruby uses for module
mix-ins and such.

The trick in both Steve's and my attempt is the really useful '-'
operator/method on Arrays, which does array difference. So useful! See
also '&' for array intersection. Can save you a lot of hacky code if you
remember they're there.

Jonathan
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top