constants and const_get

T

Timothy Hunter

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?
 
L

Logan Capaldo

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
 
T

Timothy Hunter

Logan said:
#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

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.'
 
L

Logan Capaldo

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top