Estou rodando o sistema exatamente do jeito q está abaixo e o terminal está me retornando wrong number of arguments no método book_best_selling ou em qualquer outro método q eu queria como saída de dados. Alguma ideia do pq?
Livro
class Book
attr_reader :title, :price, :year_launch, :publisher, :type
def initialize(title, price, year_launch, include_reprint, publisher, type)
@title = title
@year_launch = year_launch
@include_reprint = include_reprint
@price = calculates_price(price)
@publisher = publisher
@type = type
end
def include_reprint?
@include_reprint
end
def to_csv
"#{@title},#{@year_launch},#{@price}"
end
private
def calculates_price(base)
if @year_launch < 2006
if @include_reprint
base * 0.9
else
base * 0.95
end
elsif @year_launch <= 2010
base * 0.96
else
base
end
end
end
Sistema
require_relative "book"
require_relative "storage"
def book_for_newsletter(book)
if book.year_launch < 1999
puts "Newsletter/Liquidação"
puts book.title
puts book.price
puts book.include_reprint?
end
end
algorithms = Book.new("Algoritimos", 100, 1998, true, "", "Livro")
architecture = Book.new("Introducao a Arquitetura e Design de Software",
70, 2011, true, "", "Livro")
programmer = Book.new("The Progmatic Programer", 100, 1999, true, "", "Livro")
ruby = Book.new("Progamming Ruby", 100, 2004, true, "", "Livro")
revistona = Book.new("Magazine Ruby", 12, 2012, true, "Revistas", "revista")
# online_archutecture = Book.new("Introducao a Arquitetura e Design de Software", 70, 2011, true, "", "ebook")
storage = Storage.new
storage << algorithms << algorithms << ruby << programmer << architecture << ruby << ruby << revistona << revistona
storage.sale ruby
storage.sale algorithms
storage.sale algorithms
storage.sale ruby
storage.sale ruby
storage.sale revistona
puts storage.book_best_selling(&:title).title
Estoque
require_relative "cont"
class Storage
attr_reader :books
def initialize
@books = []
@sales = []
@books.extend Cont
end
def sale_amount(product, &field)
@sales.count {|sale| field.call(sale) == field.call(product)}
end
def book_best_selling(field)
best_selling("livro", &field)
end
def magazine_best_selling(field)
best_selling("revista", &field)
end
def best_selling(type, &field)
@sales.select { |l| l.type == type}.sort {|v1,v2|
sale_amount(v1, &field) <=>
sale_amount(v2, &field)
}.last
end
def export_csv
@books.each do |book|
puts book.to_csv
end
end
def cheaper_than(value)
@books.select do |book|
book.price <= value
end
end
def total
@books.size
end
def <<(book)
@books << book if book
self
end
def sale(book)
@books.delete book
@sales << book
end
def max_required
@books.max_required
end
end
Contador
module Cont
def <<(book)
push(book)
if @max_required.nil? || @max_required < size
@max_required = size
end
self
end
def max_required
@max_required
end
attr_reader :max_required
end