Hi. Consider:
module Foo
def self.bar
end
def ble
end
end
I need to get all methods defined in module Foo, including ble() and
Foo.bar()
But only these, no other methods!
With Foo.methods I sadly also get other methods, which makes this a bit
useless to answer my question “which methods does this module bring in?”
quik77
2
On 02/05/2014 01:13 AM, Marc H. wrote:
I need to get all methods defined in module Foo, including ble() and
Foo.bar()
But only these, no other methods!
With Foo.methods I sadly also get other methods, which makes this a bit
useless to answer my question “which methods does this module bring in?”
$ irb
2.1.0 :001 > module Foo
2.1.0 :002?> def self.bar; end
2.1.0 :003?> def ble; end
2.1.0 :004?> end
=> :ble
2.1.0 :005 > Foo.methods(false)
=> [:bar]
2.1.0 :006 > Foo.instance_methods(false)
=> [:ble]
http://rdoc.info/stdlib/core/Object:methods
http://rdoc.info/stdlib/core/Module:instance_methods
-Justin
quik77
4
Hmm… I lose the information here which one is a class method and which
one is not.
Am I right in assuming that Foo.methods(false) would return only class
methods?
quik77
5
On 02/05/2014 01:36 AM, Marc H. wrote:
Hmm… I lose the information here which one is a class method and which
one is not.
Am I right in assuming that Foo.methods(false) would return only class
methods?
Yes, as far as I know. This includes “singleton classes” for instances
of a class.
2.1.0 :018 > a = A.new
=> #<A:0x00000001f8d4e0>
2.1.0 :019 > class << a
2.1.0 :020?> def z
2.1.0 :021?> end
2.1.0 :022?> end
=> :z
2.1.0 :023 > a.methods(false)
=> [:z]
-Justin
quik77
6
Ok thanks! That should suffice to build a little convenience method that
will output ‘Foo.bar’ and ‘bar’ as strings in an array output.