Reputation: 3
I have a hash that has been parsed from HTML
The hash looks like this except it has 38 nested hashes
@ProductionInfoDataHash = {
0=>{:name=>"Name", :primSegment=>"Primary Segment", :unitsSold=>"Units Sold", :unitsInventory=>"Unit Inven tory", :revisionDate=>"Revision Date", :age=>0.0, :mtbf=>"MTBF", :pfmnCoord=>0.0, :sizeCoord=>0.0, :price=>"rice", :matirialCost=>"aterial Cost", :laborCost=>"abor Cost", :contrMarg=>0.0, :overtime=>0.02, :automation=>"Auto mation Next Round", :capacity=>"Capacity Next Round", :plantUtiliz=>0.0},
1=>{:name=>"", :primSegment=>"", :unitsSold=>"", :unitsInventory=>"", :revisionDate=>"", :age=>0.0, :mtbf=>"", :pfmnCoord=>0.0, :sizeCoord=>0.0, :price=>nil, :matirialCost=>nil, :laborCost=>nil, :contrMarg=>0.0, :overtime=>0.0, :automation=>"", :capacity=>"", :plantUtiliz=>0.0},
2=>{:name=>"Able", :primSegment=>"Trad", :unitsSold=>"999", :unitsInventory=>"189", :revisionDate=>"11/21/2015", :age=>3.1, :mtbf=>"17500", :pfmnCoord=>5.5, :sizeCoord=>14.5, :price=>"28.00", :matirialCost=>"11.59", :laborCost=>"7.49", :contrMarg=>0.29, :overtime=>0.0, :automation=>"4.0", :capacity=>"1,800", :plantUtiliz=>0.66},
3=>{:name=>"Acre", :primSegment=>"Low", :unitsSold=>"1,763", :unitsInventory=>"39", :revisionDate=>"5/25/2014", :age=>4.6, :mtbf=>"14000", :pfmnCoord=>3.0, :sizeCoord=>17.0, :price=>"21.00", :matirialCost=>"7.81", :laborCost=>"7.12", :contrMarg=>0.27, :overtime=>0.3, :automation=>"5.0", :capacity=>"1,400", :plantUtiliz=>1.29},
4=>{:name=>"Adam", :primSegment=>"High", :unitsSold=>"366", :unitsInventory=>"40", :revisionDate=>"4/19/2017", :age=>1.7, :mtbf=>"23000", :pfmnCoord=>8.0, :sizeCoord=>12.0, :price=>"38.00", :matirialCost=>"15.98", :laborCost=>"8.57", :contrMarg=>0.33, :overtime=>0.0, :automation=>"3.0", :capacity=>"900", :plantUtiliz=>0.45}
}
I want to iterate over the nested hashes based on the value of the :primSegment key.
the relevant part of my code is
def productionInfoData_to_Sheet
g = @ProductionInfoDataHash.select{ |item| item[:primSegment] == "Trad" } # get all nested hashes that have a [:primSegment] == "Trad"
puts g
end
I get the error `[]': no implicit conversion of Symbol into Integer (TypeError)
Upvotes: 0
Views: 1948
Reputation: 181
You need to consider the key and value on your select query.
Like
@ProductionInfoDataHash.select { |key, value| value[:primSegment] == 'Trad' }
Also, I recommend you to check the most used ruby style guide
you shouldn't use CamelCase on variables like ProductionInfoDataHash
Upvotes: 1
Reputation: 3168
You are not including the key in the select statement, try this
@ProductionInfoDataHash.select{ |index, item| item[:primSegment] == "Trad" }
Upvotes: 1