Constants and const_get

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?

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

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.’

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.’

On 8/28/06, Logan C. [email protected] 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