On 11.08.2009 21:45, David Sainte-claire wrote:
> I am trying to do something that on the surface seems easy, but in
> practice I don't have a clue how to approach it. I need to create a
> multi-dimensional hash to represent certain attributes of a particular
> item. When I hardcode the hash for testing purposes it looks something
> like this:
>
> @skuList = [{:sku => "XXXX",
rice => "$ZZZZ.ZZ"}, {:sku => "YYYY",
>
rice => "$AAA.AA"}]
First of all @skuList is not a Hash but an Array.
> However, in production I need to be able to generate an almost exact
> same hash dynamically, with the exception of that I don't know how many
> objects will need to be inserted into the hash.
Is the number of objects unknown or the level of nesting?
> Basically, I will be reading in a file that contains an unknown number
> of item names and need to return hash that contains the above
> attributes for all entries in the file that I just read.
Ok, you do not need arbitrary nesting but rather you want to add a
variable number of items to the Array.
> Unfortunately, I am very new to Ruby, and don't have a clue where to
> start on something like this.
You should have a look at the documentation of class Array. You'll find
methods for changing an Array's content there.
Btw, a few other remarks:
In Ruby we conventionally use identifiers like @sku_list instead of
@skuList which you rather find in Java. While of course you are free to
choose it may make your code easier readable for your fellow Ruby
programmers if you go with that convention.
If your set of attributes is fixed as seems to be the case it is a good
idea to use a class for this instead of Hash instances. The easiest
would be
SkuItem = Struct.new :sku,

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