[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

enumeration with symbol name information

Sasa

7/20/2007 9:51:00 AM

Hello everybody...
I have a beginne's question:

I've created class for my numeric constants, it looks like:

class MyClass

NAME1 = 0
NAME2 = 1
NAME3 = 2
#etc...

def self.to_s(something)
case something
when 0
"NAME1"
when 1
"NAME2"
when 2
"NAME3"
#etc...
end
end

end


What is more elegant way to do this? Basically, I want to convert the
numerical value to string representation (ideally via identifier name).

Thanks in advance,
Sasa


19 Answers

Mariusz Pekala

7/20/2007 11:18:00 AM

0

On 2007-07-20 19:00:04 +0900 (Fri, Jul), Sasa wrote:
> Hello everybody...
> I have a beginne's question:
>
> I've created class for my numeric constants, it looks like:
>
> class MyClass
>
> NAME1 = 0
> NAME2 = 1
> NAME3 = 2
> #etc...
>
> def self.to_s(something)
> case something
> when 0
> "NAME1"
> when 1
> "NAME2"
> when 2
> "NAME3"
> #etc...
> end
> end
>
> end
>
>
> What is more elegant way to do this? Basically, I want to convert the
> numerical value to string representation (ideally via identifier name).
>
> Thanks in advance,
> Sasa

If your numeric values always start from 0, then you may use an array:

class MyClass
NAMES = [ 'NAME1','NAME2','NAME3' ]

def self.to_s(something) # personally I would rather avoid redefining class.to_s
NAMES[something.to_i] || default_value
end
end

If this is unacceptable, you may use a hash:

NAMES = { 1 => 'NAME1', 2 => 'NAME2', 5 => 'NAME3' }
NUMBERS = Hash[ *NAMES.map {|k,v| [v,k]}.flatten ] # may be useful :-)

def self.to_s(something)
NAMES[ something ] || defaul_value
end

def self.to_i(something) # may be useful :-)
NUMBERS[ something ] || default_number
end

--
No virus found in this outgoing message.
Checked by 'grep -i virus $MESSAGE'
Trust me.

Anim8rFSK

9/5/2012 5:39:00 PM

0

In article <k27vop$ddu$1@dont-email.me>,
Arthur Lipscomb <arthur@alum.calberkeley.org> wrote:

> On 9/5/2012 4:26 AM, Obveeus wrote:
> > "Ubiquitous" <weberm@polaris.net> wrote:
> >
> >> What did you watch?
> >
> >
>
>
> Covert Affairs - ?Glass Spider? I don't think I followed this one as
> well as I should have. Annie reveals to Simon she's CIA and he reveals
> to her he already knew.

And claims he'd always known, but of course he might be lying

She tries to convince him to turn himself in
> but he makes her fall in love with him instead. Pretty good episode
> ruined by next week's preview.

Hmm. Really? I didn't think the preview spoiled anything.
>
>
> Revolution (OnDemand) - Others have already covered this one. I tried
> to watch with an open mind, I really did but when that plane fell
> straight down from the sky...None of the complaints or issues that
> popped up from the trailer were resolved in the pilot. Perhaps as the
> series progresses something that makes sense will occur, but I won't
> hold my breath waiting for it.

Oh it's bad. I may need to go into full snark mode on this one.

--
"Every time a Kardashian gets a TV show, an angel dies."

Arthur Lipscomb

9/5/2012 7:17:00 PM

0

On 9/5/2012 10:39 AM, anim8rFSK wrote:
> In article <k27vop$ddu$1@dont-email.me>,
> Arthur Lipscomb <arthur@alum.calberkeley.org> wrote:
>
>> On 9/5/2012 4:26 AM, Obveeus wrote:
>>> "Ubiquitous" <weberm@polaris.net> wrote:
>>>
>>>> What did you watch?
>>>
>>>
>>
>>
>> Covert Affairs - ?Glass Spider? I don't think I followed this one as
>> well as I should have. Annie reveals to Simon she's CIA and he reveals
>> to her he already knew.
>
> And claims he'd always known, but of course he might be lying
>
> She tries to convince him to turn himself in
>> but he makes her fall in love with him instead. Pretty good episode
>> ruined by next week's preview.
>
> Hmm. Really?

We now know she lives (yeah not a surprise) and apparently Lena really
thinks Annie is a traitor. I originally thought Lena and Annie were
plotting together (perhaps they still are).

I didn't think the preview spoiled anything.
>>

Perhaps not spoiled but it narrowed down the speculation. At the end of
a Batman cliffhanger, I don't want a preview for next week showing that
he escaped the trap.


>>
>> Revolution (OnDemand) - Others have already covered this one. I tried
>> to watch with an open mind, I really did but when that plane fell
>> straight down from the sky...None of the complaints or issues that
>> popped up from the trailer were resolved in the pilot. Perhaps as the
>> series progresses something that makes sense will occur, but I won't
>> hold my breath waiting for it.
>
> Oh it's bad. I may need to go into full snark mode on this one.
>


The sad thing is, unlike The Mindy Project, I'm actually going to watch
this turkey every week. :-( If only I had the willpower not to.

Anim8rFSK

9/5/2012 7:36:00 PM

0

In article <k288ee$ahj$1@dont-email.me>,
Arthur Lipscomb <arthur@alum.calberkeley.org> wrote:

> On 9/5/2012 10:39 AM, anim8rFSK wrote:
> > In article <k27vop$ddu$1@dont-email.me>,
> > Arthur Lipscomb <arthur@alum.calberkeley.org> wrote:
> >
> >> On 9/5/2012 4:26 AM, Obveeus wrote:
> >>> "Ubiquitous" <weberm@polaris.net> wrote:
> >>>
> >>>> What did you watch?
> >>>
> >>>
> >>
> >>
> >> Covert Affairs - ?Glass Spider? I don't think I followed this one as
> >> well as I should have. Annie reveals to Simon she's CIA and he reveals
> >> to her he already knew.
> >
> > And claims he'd always known, but of course he might be lying
> >
> > She tries to convince him to turn himself in
> >> but he makes her fall in love with him instead. Pretty good episode
> >> ruined by next week's preview.
> >
> > Hmm. Really?
>
> We now know she lives (yeah not a surprise)

Okay, it proved that she wasn't wearing the vest from earlier (if she
had been, I'd have quit watching on the spot)

and apparently Lena really
> thinks Annie is a traitor. I originally thought Lena and Annie were
> plotting together (perhaps they still are).

Oh. Interesting. I assumed Lena was really the traitor.
>
> I didn't think the preview spoiled anything.
> >>
>
> Perhaps not spoiled but it narrowed down the speculation. At the end of
> a Batman cliffhanger, I don't want a preview for next week showing that
> he escaped the trap.

Now that they show the episodes back to back, you don't have to wait! :D
>
>
> >>
> >> Revolution (OnDemand) - Others have already covered this one. I tried
> >> to watch with an open mind, I really did but when that plane fell
> >> straight down from the sky...None of the complaints or issues that
> >> popped up from the trailer were resolved in the pilot. Perhaps as the
> >> series progresses something that makes sense will occur, but I won't
> >> hold my breath waiting for it.
> >
> > Oh it's bad. I may need to go into full snark mode on this one.
> >
>
>
> The sad thing is, unlike The Mindy Project, I'm actually going to watch
> this turkey every week. :-( If only I had the willpower not to.

I blame myself.

--
"Every time a Kardashian gets a TV show, an angel dies."

icebreaker

9/5/2012 7:39:00 PM

0

"Arthur Lipscomb" <arthur@alum.calberkeley.org> wrote in message
news:k288ee$ahj$1@dont-email.me...
> On 9/5/2012 10:39 AM, anim8rFSK wrote:
>> In article <k27vop$ddu$1@dont-email.me>,
>> Arthur Lipscomb <arthur@alum.calberkeley.org> wrote:
>>
>>> On 9/5/2012 4:26 AM, Obveeus wrote:
>>>> "Ubiquitous" <weberm@polaris.net> wrote:
>>>>
>>>>> What did you watch?
>>>>
>>>>
>>>
>>>
>>> Covert Affairs - ?Glass Spider? I don't think I followed this one as
>>> well as I should have. Annie reveals to Simon she's CIA and he reveals
>>> to her he already knew.
>
> We now know she lives (yeah not a surprise) and apparently Lena really
> thinks Annie is a traitor. I originally thought Lena and Annie were
> plotting together (perhaps they still are).

Maybe Lena is a baddie inside the CIA and killed the Russian spy and shot
Annie to cover up what might be found with further investigation. There's no
way Arthur would have approved wet work on her without hearing her side.
Maybe she was just jealous of Annie. The shooting points to Lena being a
baddie imho unless this is a total fakeout. "Nina" (Lena) has a history of
being bad and a traitor. :)

Arthur Lipscomb

9/5/2012 9:50:00 PM

0

On 9/5/2012 12:39 PM, icebreaker wrote:
> "Arthur Lipscomb" <arthur@alum.calberkeley.org> wrote in message
> news:k288ee$ahj$1@dont-email.me...
>> On 9/5/2012 10:39 AM, anim8rFSK wrote:
>>> In article <k27vop$ddu$1@dont-email.me>,
>>> Arthur Lipscomb <arthur@alum.calberkeley.org> wrote:
>>>
>>>> On 9/5/2012 4:26 AM, Obveeus wrote:
>>>>> "Ubiquitous" <weberm@polaris.net> wrote:
>>>>>
>>>>>> What did you watch?
>>>>>
>>>>>
>>>>
>>>>
>>>> Covert Affairs - ?Glass Spider? I don't think I followed this one as
>>>> well as I should have. Annie reveals to Simon she's CIA and he reveals
>>>> to her he already knew.
>>
>> We now know she lives (yeah not a surprise) and apparently Lena really
>> thinks Annie is a traitor. I originally thought Lena and Annie were
>> plotting together (perhaps they still are).
>
> Maybe Lena is a baddie inside the CIA and killed the Russian spy and
> shot Annie to cover up what might be found with further investigation.


It never occurred to me that she was a traitor. But that does seem like
a very plausible explanation.

> There's no way Arthur would have approved wet work on her without
> hearing her side. Maybe she was just jealous of Annie.

Jealously really doesn't rise to the level of murder under the
circumstances. Plus she didn't have anything to be jealous about.

The shooting
> points to Lena being a baddie imho unless this is a total fakeout.

That's what I was suspecting *before* the shooting. But I don't see the
point in shooting *both* of them. Shooting one, sure, but not both. I
just don't see the angle in that.

Then again some of the actions simply don't make sense to me. I
remember being particular annoyed at the mall scene when they parked the
undercover van right in front of the entrance then Annie emerged from
it. I was thinking shouldn't she have a cover story for how she got
there and how she's going to get back. And what if he parked his car
right next to theirs (they were parked in the front after all) and saw
her exit the vehicle.


Jeremy

9/5/2012 11:13:00 PM

0

On Wed, 05 Sep 2012 12:16:30 -0700, Arthur Lipscomb
<arthur@alum.calberkeley.org> wrote:

>On 9/5/2012 10:39 AM, anim8rFSK wrote:
>> Arthur Lipscomb <arthur@alum.calberkeley.org> wrote:
>>> Revolution (OnDemand) - Others have already covered this one. I tried
>>> to watch with an open mind, I really did but when that plane fell
>>> straight down from the sky...None of the complaints or issues that
>>> popped up from the trailer were resolved in the pilot. Perhaps as the
>>> series progresses something that makes sense will occur, but I won't
>>> hold my breath waiting for it.
>>
>> Oh it's bad. I may need to go into full snark mode on this one.
>
>The sad thing is, unlike The Mindy Project, I'm actually going to watch
>this turkey every week. :-( If only I had the willpower not to.

Nothing wrong with that, lots of people hate-watch shows just to snark
about it later.

Fortunately for me, I'm dumb enough not to care that planes fall out
from the sky, so I'll still be entertained by it. I'm mostly just
bummed that I won't get to see more Elizabeth Mitchell.

David Barnett

9/5/2012 11:13:00 PM

0

In article <k27vop$ddu$1@dont-email.me>,
arthur@alum.calberkeley.org says...
>
> On 9/5/2012 4:26 AM, Obveeus wrote:
> > "Ubiquitous" <weberm@polaris.net> wrote:
> >
> >> What did you watch?
>
> Covert Affairs - ?Glass Spider? I don't think I followed this one as
> well as I should have. Annie reveals to Simon she's CIA and he reveals
> to her he already knew. She tries to convince him to turn himself in
> but he makes her fall in love with him instead. Pretty good episode
> ruined by next week's preview.

Have seen all that, but not the preview.

Jai may no longer be in the show, but the writers are
certainly milking his previous involvement for all its
worth.
--
David Barnett

Thanatos

9/6/2012 1:50:00 AM

0

In article <anim8rfsk-ED3AB3.10392205092012@news.easynews.com>,
anim8rFSK <anim8rfsk@cox.net> wrote:

> In article <k27vop$ddu$1@dont-email.me>,
> Arthur Lipscomb <arthur@alum.calberkeley.org> wrote:
>
> > On 9/5/2012 4:26 AM, Obveeus wrote:
> > > "Ubiquitous" <weberm@polaris.net> wrote:
> > >
> > >> What did you watch?
> > >
> > >
> >
> >
> > Covert Affairs - ?Glass Spider? I don't think I followed this one as
> > well as I should have. Annie reveals to Simon she's CIA and he reveals
> > to her he already knew.
>
> And claims he'd always known, but of course he might be lying
>
> She tries to convince him to turn himself in
> > but he makes her fall in love with him instead. Pretty good episode
> > ruined by next week's preview.
>
> Hmm. Really? I didn't think the preview spoiled anything.
> >
> >
> > Revolution (OnDemand) - Others have already covered this one. I tried
> > to watch with an open mind, I really did but when that plane fell
> > straight down from the sky...None of the complaints or issues that
> > popped up from the trailer were resolved in the pilot. Perhaps as the
> > series progresses something that makes sense will occur, but I won't
> > hold my breath waiting for it.
>
> Oh it's bad. I may need to go into full snark mode on this one.

You mean you haven't been up till now?

This oughta be good!

Thanatos

9/6/2012 1:51:00 AM

0

In article <epmf48paj74c1bcsqur0de2diii51ctpue@4ax.com>,
Jeremy <jeremys@supernews.spamtrap> wrote:

> On Wed, 05 Sep 2012 12:16:30 -0700, Arthur Lipscomb
> <arthur@alum.calberkeley.org> wrote:
>
> >On 9/5/2012 10:39 AM, anim8rFSK wrote:
> >> Arthur Lipscomb <arthur@alum.calberkeley.org> wrote:
> >>> Revolution (OnDemand) - Others have already covered this one. I tried
> >>> to watch with an open mind, I really did but when that plane fell
> >>> straight down from the sky...None of the complaints or issues that
> >>> popped up from the trailer were resolved in the pilot. Perhaps as the
> >>> series progresses something that makes sense will occur, but I won't
> >>> hold my breath waiting for it.
> >>
> >> Oh it's bad. I may need to go into full snark mode on this one.
> >
> >The sad thing is, unlike The Mindy Project, I'm actually going to watch
> >this turkey every week. :-( If only I had the willpower not to.
>
> Nothing wrong with that, lots of people hate-watch shows just to snark
> about it later.

L.A. COMPLEX... someone is calling your name!