Skip to content

Instantly share code, notes, and snippets.

View mayra-cabrera's full-sized avatar
💃
Nothing to do here. Go to https://gitlab.com/mayra-cabrera instead

Mayra Cabrera mayra-cabrera

💃
Nothing to do here. Go to https://gitlab.com/mayra-cabrera instead
View GitHub Profile
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
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?
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"..>,
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
def report_scores x
x.score
end
class School
attr_accessor :classes
def score
classes.map(&:score).avg
end
end
class Classroom
attr_accessor :students
def score
students.map(&:score).avg
end
end
class Student
attr_accessor :score
end
@mayra-cabrera
mayra-cabrera / report_scores.rb
Created August 22, 2015 22:53
report_scores
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
@mayra-cabrera
mayra-cabrera / gilden_rose.rb
Created August 22, 2015 22:02
Gilden rose code
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