31e2b397e15442a2d3ba42db7bd4584e

I would like to create an array from the details variable below that contains only the values of the subtotals below. In other words, I would like to have this when all is said and done:
subtotals = ["11.90", "11.25", "169.75", "2160.00"]

details = [{:amount=>"5.95", :subtotal=>"11.90",    :menu_label=>"Grommets",    :qty=>"2"}, 
           {:amount=>"1.25", :subtotal=>"11.25",    :menu_label=>"Stuff",       :qty=>"9"}, 
           {:amount=>"33.95", :subtotal=>"169.75",  :menu_label=>"More Stuff",  :qty=>"5"},
           {:amount=>"80.00", :subtotal=>"2160.00", :menu_label=>"HTML Markup", :qty=>"27"}]

Refactorings

No refactoring yet !

F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

August 4, 2009, August 04, 2009 02:16, permalink

1 rating. Login to rate!
details.map { |hash| hash[:subtotal] }
72820eaf703cd07ba9bc6ecc09e5d81a

Alec Leitner

August 4, 2009, August 04, 2009 02:16, permalink

No rating. Login to rate!

is this what you want?

details.map{|x| x[:subtotal]}
31e2b397e15442a2d3ba42db7bd4584e

Chip Castle

August 4, 2009, August 04, 2009 13:54, permalink

No rating. Login to rate!

Perfect! Exactly what I needed. Thanks!

Your refactoring





Format Copy from initial code

or Cancel