[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: [QUIZ][SOLUTION] Mexican Blanket (#127

James Gray

6/11/2007 1:20:00 PM

On Jun 10, 2007, at 10:39 AM, Will Bailey wrote:

> Any feedback on how I can improve this code would be much appreciated.

One idea came to me on a quick glance at the code...

> ----------------------------------------------------------------------
> ---------------------------------------
> @colors =
> Hash
> ["0","black","1","red","2","orange","3","yellow","4","green","5","blue
> ","6","indigo","7","violet","8","white","9","grey"]

First, you can drop a lot of punctuation from a line like this using
Ruby's word Array syntax:

@colors = Hash[ *%w[ 0 black 1 red 2 orange 3 yellow
4 green 5 blue 6 indigo 7 violet
8 white 9 grey ] ]

Of course, those keys are just indices, and we should probably treat
them as such:

require "enumerator"
@color = Hash[ *%w[ black red orange yellow green
blue indigo violet white gray ].
enum_with_index.to_a.flatten.reverse ]

I do realize that's not quite as readable though.

James Edward Gray II

1 Answer

James Gray

6/11/2007 2:49:00 PM

0

On Jun 11, 2007, at 9:44 AM, Will Bailey wrote:

> Now it seems like I could have just used an array. Originally I
> was looking
> at using the letters to key the colors, which is why I was thinking
> hash.
> Isn't the result of your second suggestion almost an array in hash
> clothing?

It is. That's a very good point.

James Edward Gray II