Skip to content

Instantly share code, notes, and snippets.

@mayra-cabrera
Last active December 15, 2015 22:40
Show Gist options
  • Save mayra-cabrera/3ff1f72a5130b06f6bf9 to your computer and use it in GitHub Desktop.
Save mayra-cabrera/3ff1f72a5130b06f6bf9 to your computer and use it in GitHub Desktop.
class GuaranteedProduct
def self.find(id)
Product.find(id) || MissingProduct.new
end
end
product_ids = [123,456,'',789]
products = product_ids.map{|id| GuaranteedProduct.find(id)}
=> [#<Product:0x000123123 @name="TV"..>,
#<Product:0x000123124 @name="Cellphone"..>,
#<MissingProduct:>,
#<Product:0x000123125 @name="iPad"..>
]
products.each do |product|
puts product.name
end
# "TV"
# "Cellphone"
# "no product"
# "iPad"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment