Languange: Ruby
Framework: Ruby on Rails
Requirement: Remove duplicate Product Descriptions for display on an eCommerce web page.
Solution: Create a method in the Products helper class called dedup_product_descriptions containing this code:
@products = Hash[*(@products).map{|o|[o.description,o]}.flatten].values
and add this code to the Products view:
<% dedup_product_descriptions() %>
To make it even more interesting, let’s say in the case of duplicate descriptions you are required to display the description associated with the most expensive product. No problem. Use this code in the helper method instead of the code above:
@products = @products.sort{|a,b|a.price<=>b.price}@products = Hash[*(@products).map{|o|[o.description,o]}.flatten].values
Chad Dalton






test