[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

constants and const_get

Tim Hunter

8/28/2006 12:43:00 AM

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?

3 Answers

Logan Capaldo

8/28/2006 1:10:00 AM

0


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


Tim Hunter

8/28/2006 1:55:00 AM

0

Logan Capaldo wrote:
>
> #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.'

Logan Capaldo

8/28/2006 2:07:00 AM

0


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

> Logan Capaldo wrote:
>>
>> #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,
>
>

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