Sai Prashanth
Sai Prashanth

Reputation: 57

How do you get the list of objects on which a method can be called?

Is there a method like +.method which can give a list of classes on which + can be called upon?

Upvotes: 0

Views: 28

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121020

There is a hack which actually guarantees nothing, but you might want to try:

▶ ObjectSpace.each_object.select { |obj| 
    Class === obj && obj.instance_methods.include?(:+) 
  }
#=> [
#  [0] Complex < Numeric,
#  [1] Rational < Numeric,
#  [2] Time < Object,
#  [3] Array < Object,
#  [4] Bignum < Integer,
#  [5] Float < Numeric,
#  [6] Fixnum < Integer,
#  [7] String < Object,
#  [8] Pathname < Object,
#  [9] CodeRay::Tokens < Array

Upvotes: 1

Related Questions