This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
product_ids = [123,456,'',789] | |
products = product_ids.map{|id| Product.find(id)} | |
=> [#<Product:0x000123123 @name="TV"..>, | |
#<Product:0x000123124 @name="Cellphone"..>, | |
nil, | |
#<Product:0x000123125 @name="iPad"..> | |
] | |
products.each do |product| | |
if product && product.name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
product_ids = [123,456,'',789] | |
products = product_ids.map{|id| Product.find(id)} | |
=> [#<Product:0x000123123 @name="TV"..>, | |
#<Product:0x000123124 @name="Cellphone"..>, | |
nil, | |
#<Product:0x000123125 @name="iPad"..> | |
] | |
products.each do |product| | |
if product.nil? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
product_ids = [123,456,'',789] | |
products = product_ids.map{|id| Product.find(id)} | |
=> [#<Product:0x000123123 @name="TV"..>, | |
#<Product:0x000123124 @name="Cellphone"..>, | |
nil, | |
#<Product:0x000123125 @name="iPad"..> | |
] | |
products.compact | |
=> [#<Product:0x000123123 @name="TV"..>, | |
#<Product:0x000123124 @name="Cellphone"..>, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
product_ids = [123,456,'',789] | |
products = product_ids.map{|id| Product.find(id)} | |
=> [#<Product:0x000123123 @name="TV"..>, | |
#<Product:0x000123124 @name="Cellphone"..>, | |
nil, | |
#<Product:0x000123125 @name="iPad"..> | |
] | |
products.each do |product| | |
puts product.name | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def report_scores x | |
x.score | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class School | |
attr_accessor :classes | |
def score | |
classes.map(&:score).avg | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Classroom | |
attr_accessor :students | |
def score | |
students.map(&:score).avg | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Student | |
attr_accessor :score | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def report_scores x | |
case x | |
when Student | |
x.score | |
when Classroom | |
x.students.map(&:score).avg | |
when School | |
x.classes.map(&:students).flatten.map(&:score).avg | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GildedRose | |
def initialize(items) | |
@items = items | |
end | |
def update_quality() | |
@items.each do |item| | |
if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert" | |
if item.quality > 0 |