Class methods

I thought this would be easy, so it's embarassing to ask.
How can I get a list of the class methods in a given class?
I know I can get public instance methods like this.

MyClass.instance_methods.sort.each {|m| puts m}

···

--
R. Mark Volkmann
Partner, Object Computing, Inc.

Class is an Object too, so

  MyClass.methods.sort.each {|m| puts m}

T.

MyClass.methods

(I think)

class Foo
  def self.class_method_foo
  end

  def public_method_foo
  end
end

Foo.methods.include? "class_method_foo" => true
Foo.methods.include? "public_method_foo" => false

···

On 9/22/05, Mark Volkmann <r.mark.volkmann@gmail.com> wrote:

I thought this would be easy, so it's embarassing to ask.
How can I get a list of the class methods in a given class?
I know I can get public instance methods like this.

MyClass.instance_methods.sort.each {|m| puts m}

Hi --

···

On Fri, 23 Sep 2005, Mark Volkmann wrote:

I thought this would be easy, so it's embarassing to ask.
How can I get a list of the class methods in a given class?
I know I can get public instance methods like this.

MyClass.instance_methods.sort.each {|m| puts m}

See other answers for main answer, but also:

   puts array

is the same as

   array.each {|e| puts e }

:slight_smile:

David

--
David A. Black
dblack@wobblini.net

The method "methods" returns ALL the methods.
I want just the class methods, those I can invoke without creating an
object from the class.
For example, querying the class Thread should give me "current", but
not "alive?".

···

On 9/22/05, Trans <transfire@gmail.com> wrote:

Class is an Object too, so

  MyClass.methods.sort.each {|m| puts m}

--
R. Mark Volkmann
Partner, Object Computing, Inc.

Mark Volkmann wrote:

···

On 9/22/05, Trans <transfire@gmail.com> wrote:

Class is an Object too, so

  MyClass.methods.sort.each {|m| puts m}

The method "methods" returns ALL the methods.
I want just the class methods, those I can invoke without creating an
object from the class.
For example, querying the class Thread should give me "current", but
not "alive?".

If you want only the methods that are specific to Thread:

   Thread.methods - Thread.class.methods

(I'm new to ruby, so forgive me if this is not the correct approach)

I think you're looking for Thread.singleton_methods:

irb(main):001:0> Thread.singleton_methods
=> ["stop", "abort_on_exception", "current", "new", "fork", "critical",
"abort_on_exception=", "pass", "start", "exit", "list", "critical=", "kill",
"main"]

···

On Thursday 22 September 2005 9:19, Mark Volkmann wrote:

On 9/22/05, Trans <transfire@gmail.com> wrote:
> Class is an Object too, so
>
> MyClass.methods.sort.each {|m| puts m}

The method "methods" returns ALL the methods.
I want just the class methods, those I can invoke without creating an
object from the class.
For example, querying the class Thread should give me "current", but
not "alive?".

--
Jason Voegele
You will always find something in the last place you look.

alive? is not returned from Thread.methods:

$ ruby -e 'p Thread.methods.grep(/alive/)'

Robert@Babelfish2 ~
$ ruby -e 'Thread.methods.each {|m| puts m unless Thread.respond_to? m}'

Robert@Babelfish2 ~

Thread actually *does* respond to all these methods.

As Jason pointed out you're probably looking for Thread.singleton_methods.

Kind regards

    robert

···

Mark Volkmann <r.mark.volkmann@gmail.com> wrote:

On 9/22/05, Trans <transfire@gmail.com> wrote:

Class is an Object too, so

  MyClass.methods.sort.each {|m| puts m}

The method "methods" returns ALL the methods.
I want just the class methods, those I can invoke without creating an
object from the class.
For example, querying the class Thread should give me "current", but
not "alive?".

Didn't you see my post?

class Foo
def self.class_method_foo
end

def public_method_foo
end
end

Foo.methods.include? "class_method_foo" => true
Foo.methods.include? "public_method_foo" => false

Also,

Mac-Mini:~ joevandyk$ irb
irb(main):001:0> Thread.methods.include? "alive?"
=> false
irb(main):002:0> Thread.methods.include? "current"
=> true

···

On 9/22/05, Mark Volkmann <r.mark.volkmann@gmail.com> wrote:

On 9/22/05, Trans <transfire@gmail.com> wrote:
> Class is an Object too, so
>
> MyClass.methods.sort.each {|m| puts m}

The method "methods" returns ALL the methods.
I want just the class methods, those I can invoke without creating an
object from the class.
For example, querying the class Thread should give me "current", but
not "alive?".

Jason Voegele wrote:

I think you're looking for Thread.singleton_methods:

irb(main):001:0> Thread.singleton_methods
=> ["stop", "abort_on_exception", "current", "new", "fork", "critical", "abort_on_exception=", "pass", "start", "exit", "list", "critical=", "kill", "main"]

Thread.methods(false) works, too. This is new to 1.8, so you're not going to see it in the typical free documentation (PickAxe1, or ri docs).

Devin