class methods

M

Mark Volkmann

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}
 
J

Joe Van Dyk

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}

MyClass.methods

(I think)

class Foo
def self.class_method_foo
end

def public_method_foo
end
end

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

Mark Volkmann

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?".
 
B

Bob Showalter

Mark said:
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)
 
J

Jason Voegele

--nextPart2169793.ukYMV9Y3RO
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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?".

I think you're looking for Thread.singleton_methods:

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

=2D-=20
Jason Voegele
You will always find something in the last place you look.

--nextPart2169793.ukYMV9Y3RO
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQBDM1y8hnQPOwpx0DURAmqMAJ4544uA5RMSTACTFHvjqKTrnn51hgCfRul7
Kgfcc8xH92VlF+zUdU4i7N8=
=Jg5f
-----END PGP SIGNATURE-----

--nextPart2169793.ukYMV9Y3RO--
 
D

Devin Mullins

Jason said:
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
 
R

Robert Klemme

Mark Volkmann said:
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?".

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
 
D

David A. Black

Hi --

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 }

:)


David
 
J

Joe Van Dyk

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" =3D> true
Foo.methods.include? "public_method_foo" =3D> false



Also,

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

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

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,517
Latest member
Andres38A1

Latest Threads

Top