list methods only for specific class

Discussion in 'Ruby' started by Le Sa, Jun 15, 2009.

  1. Le Sa

    Le Sa Guest

    hello folks!

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

    Thank you!
    --
    Posted via http://www.ruby-forum.com/.
    Le Sa, Jun 15, 2009
    #1
    1. Advertising

  2. Le Sa

    Daniel Roux Guest

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

    On Mon, Jun 15, 2009 at 12:38 AM, Le Sa <> wrote:

    > hello folks!
    >
    > Is there a way to list methods only for specific class, i.e., not
    > inherited?
    >
    > Thank you!
    > --
    > Posted via http://www.ruby-forum.com/.
    >


    object.public_methods(false)

    Hope this helps!
    --
    Daniel Roux
    Daniel Roux, Jun 15, 2009
    #2
    1. Advertising

  3. Robert Klemme, Jun 15, 2009
    #3
  4. Le Sa

    Le Sa Guest

    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
    --
    Posted via http://www.ruby-forum.com/.
    Le Sa, Jun 15, 2009
    #4
  5. Le Sa

    Le Sa Guest

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

    Le Sa wrote:
    > I would like to list ms1 and ms2, but thanks for the help!



    --
    Posted via http://www.ruby-forum.com/.
    Le Sa, Jun 15, 2009
    #5
  6. Le Sa

    Michael Kohl Guest

    On Mon, Jun 15, 2009 at 5:27 PM, Le Sa<> wrote:
    > 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:

    >> Student.new.methods - Object.instance_methods

    =3D> ["ms1", "mp1"]
    Michael Kohl, Jun 15, 2009
    #6
  7. Le Sa

    Daniel Roux Guest

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

    On Mon, Jun 15, 2009 at 10:27 AM, Le Sa <> wrote:

    > 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
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    Try:
    Student.new.public_methods(false) + Student.new.private_methods(false)
    => ["ms1", "ms2"]
    --
    Daniel Roux
    Daniel Roux, Jun 15, 2009
    #7
  8. 2009/6/15 Daniel Roux <>:

    > 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


    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Jun 16, 2009
    #8
  9. Le Sa

    s.ross Guest

    On Jun 16, 2009, at 12:04 AM, Robert Klemme wrote:

    > 2009/6/15 Daniel Roux <>:
    >
    >> 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...
    s.ross, Jun 16, 2009
    #9
  10. Steve Ross wrote:

    > 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
    --
    Posted via http://www.ruby-forum.com/.
    Jonathan Rochkind, Jun 16, 2009
    #10
  11. Le Sa

    Le Sa Guest

    Le Sa, Jun 16, 2009
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page