In Conteggio degli uccelli hai conosciuto i metodi di enumerazione count, any?, select, all? e map.
Ecco un ripasso di quelli, con qualche metodo in più:
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"]
Enumerare oggetti Hash è esattamente come enumerare oggetti Array, con la differenza che il blocco riceve due argomenti: la chiave e il valore:
pet_names = {cat: "bob", horse: "caris", mouse: "arya"}
pet_names.each { |animal, name| ... }
Se ti serve solo uno dei valori, puoi usare il simbolo speciale _ per indicare che un valore non è necessario.
Questo aiuta sia per la chiarezza dello sviluppatore sia come ottimizzazione delle prestazioni.
pet_names = {cat: "bob", horse: "caris", mouse: "arya"}
pet_names.map { |_, name| name } #=> ["bob", "caris", "arya"]
Puoi anche enumerare in blocchi annidati e concatenare i metodi tra loro. Per esempio, se abbiamo un array di hash di animali e vogliamo estrarre gli animali con nomi brevi, potremmo fare qualcosa del genere:
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"]
Gestisci una boutique di moda online. Stanno per arrivare i grandi saldi annuali, quindi vuoi creare qualche funzionalità che ti aiuti a fare il punto sull'inventario e ad assicurarti di essere pronto.
Un singolo articolo dell'inventario è rappresentato da un hash, mentre l'intero inventario è un array di questi hash.
[
{
name: "White Shirt",
price: 40,
quantity_by_size: {s: 3, m: 7, l: 8, xl: 4}
},
{ ... }
]
Implementa BoutiqueInventory.item_names, che deve restituire un array con i nomi degli articoli in ordine alfabetico.
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"]
Implementa BoutiqueInventory.cheap, che deve restituire gli articoli che costano meno di 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}}
# ]
Implementa BoutiqueInventory.out_of_stock, che deve restituire gli articoli di cui non c'è più disponibilità (quelli in cui quantity_by_size è vuoto).
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: {}}]
Implementa BoutiqueInventory.stock_for_item(name) per restituire le scorte di un articolo a partire dal suo nome:
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}
Implementa BoutiqueInventory.total_stock per calcolare il numero totale di articoli nel magazzino:
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
Iscriviti a Exercism per imparare e padroneggiare Ruby con 21 concetti121 esercizi e il mentoring di persone reali, tutto gratis.