Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: values of constants

Tags:

ruby

I'm using

MyModule.constants.each do |c|
  #my loop
end

How do I get the VALUE of each constant rather than its name?

like image 772
themirror Avatar asked Dec 10 '22 07:12

themirror


1 Answers

You're looking for Module#const_get:

irb(main):014:0> MyModule.constants.each do |c|
irb(main):015:1*   puts(c.to_s + "  " + MyModule.const_get(c).to_s)
irb(main):016:1> end
MY_CONST  5
=> [:MY_CONST]
like image 192
Mark Rushakoff Avatar answered Jan 03 '23 18:01

Mark Rushakoff