I want to get the values of all the constants defined in a module:
module Letters
A = 'apple'.freeze
B = 'boy'.freeze
end
constants
gave me the name of the constants:
Letters.constants(false)
#=> [:A, :B]
How do I get an array of their values, i.e. ["apple", "boy"]
?
In order to do this use map
Letters.constants(false).map &Letters.method(:const_get)
this will return ["a","b"]
Second way :
Letters.constants(false).map { |c| Letters.const_get c }
Thanks @mudasobwa and Sagar Pandya for their answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With