On Aug 27, 2006, at 8:42 PM, Timothy Hunter wrote:

> Given
>
> module Foo
>   A = 1
>   B = 2
> end
>
> Imagine there's a huge list of constants defined in Foo instead of  
> just 2.
>
> I want to freeze all the constants in Foo, so I add this:
>
> module Foo
>    A = 1
>    B = 2
>    constants.each {|c| const_get(c).freeze }
> end
>
> It seems a waste to have to construct an array of constant names  
> and then call const_get on each one. Is there any way to get the  
> constants directly?
>

#constants is getting the constants directly. (What you want is the  
values of the constants.) AFAIK there is no built in way to do this.  
The code isn't terribly long though anyway is it?

class Module
   def constant_values
     constants.map { |constant| const_get(constant) }
   end
end