M.constants.map {|c| M.const_get(c)}.include?(M::C)
Or from johannes comment using find (it will work better if the class exists in M and is not the last constant in M, although it rarely needs to make a measurable difference):
M.constants.find {|c| M.const_get(c) == M::C }
Edit: since you really just want to get a boolean result, this one any?does more sending than find:
M.constants.any? {|c| M.const_get(c) == M::C }
source
share