# Constants and const\_get

**URL:** <https://www.ruby-forum.com/t/constants-and-const-get/66629>\
**Category:** Ruby\
**Created:** [August 28, 2006, 2:43am UTC](https://www.ruby-forum.com/t/constants-and-const-get/66629 "2006-08-28T02:43:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tim-hunter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/278dde/32.png) [@tim-hunter](https://www.ruby-forum.com/u/tim-hunter)\
**Post date:** [August 28, 2006, 2:43am UTC](https://www.ruby-forum.com/t/constants-and-const-get/66629/1 "2006-08-28T02:43:42Z")

</div>

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©.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?

---

<div class="post-metadata">

**Author:** ![tim-hunter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/278dde/32.png) [@tim-hunter](https://www.ruby-forum.com/u/tim-hunter)\
**Post date:** [August 28, 2006, 3:11am UTC](https://www.ruby-forum.com/t/constants-and-const-get/66629/2 "2006-08-28T03:11:43Z")

</div>

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

> I want to freeze all the constants in Foo, so I add this:

#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

---

<div class="post-metadata">

**Author:** ![tim-hunter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/278dde/32.png) [@tim-hunter](https://www.ruby-forum.com/u/tim-hunter)\
**Post date:** [August 28, 2006, 3:55am UTC](https://www.ruby-forum.com/t/constants-and-const-get/66629/3 "2006-08-28T03:55:44Z")

</div>

Logan C. wrote:

> 

ri says that Module#constants returns an array of constant names. For  
some reason this reminds me of this bit of _Alice Through the Looking  
Glass_, where Alice is talking to the Knight,

‘The name of the song is called "Haddocks’ Eyes".’

`Oh, that’s the name of the song, is it?’ Alice said, trying to feel  
interested.

`No, you don't understand,' the Knight said, looking a little vexed.`That’s what the name is called. The name really is “The Aged Aged  
Man”.’

`Then I ought to have said “That’s what the song is called”?’ Alice  
corrected herself.

`No, you oughtn’t: that’s quite another thing! The song is called “Ways  
and Means”: but that’s only what it’s called, you know!’

`Well, what is the song, then?’ said Alice, who was by this time  
completely bewildered.

`I was coming to that,' the Knight said.`The song really is “A-sitting  
On a Gate”: and the tune’s my own invention.‘The name of the song is  
called "Haddocks’ Eyes".’

`Oh, that’s the name of the song, is it?’ Alice said, trying to feel  
interested.

`No, you don't understand,' the Knight said, looking a little vexed.`That’s what the name is called. The name really is “The Aged Aged  
Man”.’

`Then I ought to have said “That’s what the song is called”?’ Alice  
corrected herself.

`No, you oughtn’t: that’s quite another thing! The song is called “Ways  
and Means”: but that’s only what it’s called, you know!’

`Well, what is the song, then?’ said Alice, who was by this time  
completely bewildered.

`I was coming to that,' the Knight said.`The song really is “A-sitting  
On a Gate”: and the tune’s my own invention.’

---

<div class="post-metadata">

**Author:** ![tim-hunter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/278dde/32.png) [@tim-hunter](https://www.ruby-forum.com/u/tim-hunter)\
**Post date:** [August 28, 2006, 4:07am UTC](https://www.ruby-forum.com/t/constants-and-const-get/66629/4 "2006-08-28T04:07:45Z")

</div>

On Aug 27, 2006, at 9:54 PM, Timothy H. wrote:

> > end
> 
> ri says that Module#constants returns an array of constant names.  
> For some reason this reminds me of this bit of _Alice Through the  
> Looking Glass_, where Alice is talking to the Knight,

[snip brilliant through the looking glass quote]

To which I reply:  
‘When I use a word,’ Humpty Dumpty said in rather a scornful tone,  
‘it means just what I choose it to mean – neither more nor less.’

‘The question is,’ said Alice, ‘whether you can make words mean so  
many different things.’  
‘The question is,’ said Humpty Dumpty, ‘which is to be master –  
that’s all.’

---

<div class="post-metadata">

**Author:** ![tim-hunter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/278dde/32.png) [@tim-hunter](https://www.ruby-forum.com/u/tim-hunter)\
**Post date:** [August 28, 2006, 7:13pm UTC](https://www.ruby-forum.com/t/constants-and-const-get/66629/5 "2006-08-28T19:13:42Z")

</div>

On 8/28/06, Logan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > class Module
> 
> ‘The question is,’ said Humpty Dumpty, ‘which is to be master –  
> that’s all.’
> 
> [snip another brilliant Lewis Carrol snippet]

‘Would you tell me, please, which way I ought to go from here?’’  
That depends a good deal on where you want to get to,  
'‘I don’t know where. . .’\*  
‘\*Then it doesn’t matter which way you go,’

Can you forgive us OP?

Robert
