在鸟类计数中,你已经认识了 count、any?、select、all? 和 map 这些枚举方法。下面回顾一下它们,并补充一些额外的方法:
fibonacci = [0, 1, 1, 2, 3, 5, 8, 13]
fibonacci.count { |number| number == 1 } #=> 2
fibonacci.any? { |number| number > 20 } #=> false
fibonacci.none? { |number| number > 20 } #=> true
fibonacci.select { |number| number.odd? } #=> [1, 1, 3, 5, 13]
fibonacci.all? { |number| number < 20 } #=> true
fibonacci.map { |number| number * 2 } #=> [0, 2, 2, 4, 6, 10, 16, 26]
fibonacci.select { |number| number >= 5 } #=> [5, 8, 13]
fibonacci.find { |number| number >= 6 } #=> 8
# Some methods work with or without a block
fibonacci.sum #=> 33
fibonacci.sum { |number| number * number } #=> 273
# There are also methods to help with nested arrays:
animals = [ ['cat', 'bob'], ['horse', 'caris'], ['mouse', 'arya'] ]
animals.flatten #=> ["cat", "bob", "horse", "caris", "mouse", "arya"]
枚举 Hash 对象和枚举 Array 对象完全一样,只不过块会接收两个实参:键和值:
pet_names = {cat: "bob", horse: "caris", mouse: "arya"}
pet_names.each { |animal, name| ... }
如果你只需要其中一个值,可以用特殊的_符号来表示不需要那个值。这既能让开发者更清楚,也能优化性能。
pet_names = {cat: "bob", horse: "caris", mouse: "arya"}
pet_names.map { |_, name| name } #=> ["bob", "caris", "arya"]
你还可以在嵌套的块中进行枚举,并把方法串起来。比如,如果我们有一个数组,里面是动物的哈希,想提取出名字较短的动物,可以这样做:
pets = [
{ animal: "cats", names: ["bob", "fred", "sandra"] },
{ animal: "horses", names: ["caris", "black beard", "speedy"] },
{ animal: "mice", names: ["arya", "jerry"] }
]
pets.map { |pet|
pet[:names].select { |name| name.length <= 5 }
}.flatten.sort
#=> ["arya", "bob", "caris", "fred", "jerry"]
你经营着一家线上时尚精品店。一年一度的大促销就要到了,你想做一些功能来帮你盘点库存,确保一切准备就绪。
库存中的每件商品用一个哈希表示,整个库存则是这些哈希组成的数组。
[
{
name: "White Shirt",
price: 40,
quantity_by_size: {s: 3, m: 7, l: 8, xl: 4}
},
{ ... }
]
实现BoutiqueInventory.item_names,它应返回按字母顺序排列的商品名称数组。
BoutiqueInventory.new([
{price: 65.00, name: "Maxi Brown Dress", quantity_by_size: {s: 3, m: 7, l: 8, xl: 4}},
{price: 50.00, name: "Red Short Skirt", quantity_by_size: {}},
{price: 29.99, name: "Black Short Skirt", quantity_by_size: {s: 1, xl: 4}},
{price: 20.00, name: "Bamboo Socks Cats", quantity_by_size: {s: 7, m: 2}}
]).item_names
#=> ["Bamboo Socks Cats", "Black Short Skirt", "Maxi Brown Dress", "Red Short Skirt"]
实现BoutiqueInventory.cheap,它应返回所有价格低于 $30 的商品。
BoutiqueInventory.new([
{price: 65.00, name: "Maxi Brown Dress", quantity_by_size: {s: 3, m: 7, l: 8, xl: 4}},
{price: 50.00, name: "Red Short Skirt", quantity_by_size: {}},
{price: 29.99, name: "Black Short Skirt", quantity_by_size: {s: 1, xl: 4}},
{price: 20.00, name: "Bamboo Socks Cats", quantity_by_size: {s: 7, m: 2}}
]).cheap
# => [
# {price: 29.99, name: "Black Short Skirt", quantity_by_size: {s: 1, xl: 4}},
# {price: 20.00, name: "Bamboo Socks Cats", quantity_by_size: {s: 7, m: 2}}
# ]
实现BoutiqueInventory.out_of_stock,它应返回所有没有库存的商品(即quantity_by_size为空)。
BoutiqueInventory.new([
{price: 65.00, name: "Maxi Brown Dress", quantity_by_size: {s: 3, m: 7, l: 8, xl: 4}},
{price: 50.00, name: "Red Short Skirt", quantity_by_size: {}},
{price: 29.99, name: "Black Short Skirt", quantity_by_size: {s: 1, xl: 4}},
{price: 20.00, name: "Bamboo Socks Cats", quantity_by_size: {s: 7, m: 2}}
]).out_of_stock
#=> [{price: 50.00, name: "Red Short Skirt", quantity_by_size: {}}]
实现BoutiqueInventory.stock_for_item(name),根据商品名称返回该商品的库存:
BoutiqueInventory.new([
{price: 65.00, name: "Maxi Brown Dress", quantity_by_size: {s: 3, m: 7, l: 8, xl: 4}},
{price: 50.00, name: "Red Short Skirt", quantity_by_size: {}},
{price: 29.99, name: "Black Short Skirt", quantity_by_size: {s: 1, xl: 4}},
{price: 20.00, name: "Bamboo Socks Cats", quantity_by_size: {s: 7, m: 2}}
]).stock_for_item("Black Short Skirt")
#=> {s: 1, xl: 4}
实现BoutiqueInventory.total_stock,计算你仓库中商品的总数量:
BoutiqueInventory.new([
{price: 65.00, name: "Maxi Brown Dress", quantity_by_size: {s: 3, m: 7, l: 8, xl: 4}},
{price: 50.00, name: "Red Short Skirt", quantity_by_size: {}},
{price: 29.99, name: "Black Short Skirt", quantity_by_size: {s: 1, xl: 4}},
{price: 20.00, name: "Bamboo Socks Cats", quantity_by_size: {s: 7, m: 2}}
]).total_stock
#=> 36