3

Here's a sample of array:

{"C1"=>[
        {:upc=>"51857195821952", :product_id=>"1234", :name=>"name", :price=>" $15 ", :color=>"green", :size=>"L", :description=>"descr"},
        {:upc=>"352353wegs", :product_id=>"456", :name=>"name2", :price=>"$21", :color=>"black", :size=>"S", :description=>"descr"}, # ...
       ],
 #...
}

And here as I am trying to fetch data from that array:

@array.each do |p|
  product = Product.new
  product.sku = p[0]
  product.name = p[1][0][:name] #can't convert Symbol into Integer
  price = p[1].select{ |pr| !pr[:price].nil? and pr[:price] != "0" }.min_by{ |i| i[:price].to_f }[:price]
  product.price = "%.2f" % (price.to_f)
  ...
end

Every time I try to fetch data from the array, I get on the line product.name = the error can't convert Symbol into Integer.

What is wrong in this case? I spent a part of afternoon on this issue, but unfortunately I still cannot figure out it...

Thanky you

0

3 Answers 3

5

Your @array is actually a hash. It is formated like following:

{ 
  'name1' => [{:upc => "..."},{:upc => "..."}],
  'name2' => [{:upc => "..."},{:upc => "..."}],
  #...
}

Since it is a Hash, you can use 2 arguments in the each (works for map also) method (one for the key, the other for the value):

@array.each do |name, array|
  product = Product.new
  product.sku = name # returns "C1"
  array.each do |data|
    data[:upc]
    data[:name]
    #etc...
  end
end
Sign up to request clarification or add additional context in comments.

2 Comments

thank you. But how can I access the price element? The current way how I am trying to obtain that generate error private method 'select' called for "\"":String
You can access to it using data[:price] but this will return a string like " 15$ ", you have to strip it and gsub the '$' before converting it into a Float
0

The fundamental problem is that the sample array you showed above is not actually an array. It's a hash with key-value pairs. Therefore, your code like p[0] or p[1][0] doesn't make sense because a hash doesn't have index like array. Hash is not ordered. Hashes values are accessed with a "key" rather than an "index" like array.

Iterating through key-value pairs of a hash is done something like this.

1.9.3p194 :001 > x = {:x => 10, :y => 9, :z => 10}
 => {:x=>10, :y=>9, :z=>10} 
1.9.3p194 :002 > x.each do |key, value|
1.9.3p194 :003 >   puts "#{key} : #{value}"
1.9.3p194 :004?> end 
x : 10
y : 9
z : 10
 => {:x=>10, :y=>9, :z=>10} 

Comments

0

It looks like you may be confusing Arrays and Hashes a bit.

Given this:

@array = {"C1"=>[
  {:upc=>"51857195821952", :product_id=>"1234", :name=>"name", :price=>" $15 ", :color=>"green", :size=>"L", :description=>"descr"}, 
  {:upc=>"352353wegs", :product_id=>"456", :name=>"name2", :price=>" $21 ", :color=>"black", :size=>"S", :description=>"descr"} 
] }

Then @array.class.name is Hash

You can get the actual array by accessing it like so:

@actual_array = @array["C1"]

Then, @actual_array.class.name will be Array

So, taking this approach and re-writing:

@array = {"C1"=>[
  {:upc=>"51857195821952", :product_id=>"1234", :name=>"name", :price=>" $15 ", :color=>"green", :size=>"L", :description=>"descr"}, 
  {:upc=>"352353wegs", :product_id=>"456", :name=>"name2", :price=>" $21 ", :color=>"black", :size=>"S", :description=>"descr"} 
 ] }

@actual_array = @array["C1"]

@actual_array.each do |p|
  puts p[:name]
end

If you do this, you'll find that the value of the :name element will be printed neatly out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.