Charles Hixson | 16 Aug 2012 23:37
Picon
Favicon

:symbol vs CONSTANT choice

I have a list of about 12 items that I wish to repeatedly store as part 
of a record in a file.  The obvious way to do this is to define a bunch 
of constant terms with values [1 .. 12].  But when I want to use them in 
the program, symbols would be more convenient.  Is there a better way to 
do this than just defining the constants, and using a pair of functions 
to convert to and from symbols?

--

-- 
Charles Hixson

Robert Klemme | 17 Aug 2012 16:50
Gravatar

Re: :symbol vs CONSTANT choice

On Thu, Aug 16, 2012 at 11:37 PM, Charles Hixson
<charleshixsn <at> earthlink.net> wrote:
> I have a list of about 12 items that I wish to repeatedly store as part of a
> record in a file.  The obvious way to do this is to define a bunch of
> constant terms with values [1 .. 12].  But when I want to use them in the
> program, symbols would be more convenient.  Is there a better way to do this
> than just defining the constants, and using a pair of functions to convert
> to and from symbols?

There are some approaches to do enums in Ruby out there, e.g.
http://code.dblock.org/how-to-define-enums-in-ruby
http://stackoverflow.com/questions/75759/enums-in-ruby
http://www.lesismore.co.za/rubyenums.html

You'll find plenty more with your favorite search engine.

Other than that the two function approach probably works as well.  Or
you use two Hashes for both directions and not two functions.

Kind regards

robert

--

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Charles Hixson | 19 Aug 2012 08:32
Picon
Favicon

Re: :symbol vs CONSTANT choice

On 08/17/2012 07:50 AM, Robert Klemme wrote:
> On Thu, Aug 16, 2012 at 11:37 PM, Charles Hixson
> <charleshixsn <at> earthlink.net>  wrote:
>    
>> I have a list of about 12 items that I wish to repeatedly store as part of a
>> record in a file.  The obvious way to do this is to define a bunch of
>> constant terms with values [1 .. 12].  But when I want to use them in the
>> program, symbols would be more convenient.  Is there a better way to do this
>> than just defining the constants, and using a pair of functions to convert
>> to and from symbols?
>>      
> There are some approaches to do enums in Ruby out there, e.g.
> http://code.dblock.org/how-to-define-enums-in-ruby
> http://stackoverflow.com/questions/75759/enums-in-ruby
> http://www.lesismore.co.za/rubyenums.html
>
> You'll find plenty more with your favorite search engine.
>
> Other than that the two function approach probably works as well.  Or
> you use two Hashes for both directions and not two functions.
>
> Kind regards
>
> robert
>    
Thanks.  I pulled two versions of enum out of those links, that look 
like they might be what I want.  It will be a couple of weeks yet, and I 
don't know whether the bit manipulation version (class) will be what I 
need or whether I'll go with the simpler (module) version.  One of them 
will clearly be more suited to the situation than just defining a bunch 
(Continue reading)


Gmane