Skip to content

Instantly share code, notes, and snippets.

@BrayanZ
Created May 3, 2013 01:53
Show Gist options
  • Save BrayanZ/5506721 to your computer and use it in GitHub Desktop.
Save BrayanZ/5506721 to your computer and use it in GitHub Desktop.
class Invoice
def initialize *lines
@lines = lines
end
def total
@lines.inject(0){ |subtotal, line| subtotal += line_subtotal(line) }
end
def line_subtotal line
line[:quantity] * line[:price]
end
end
invoice = Invoice.new({quantity: 3, product_name: "ice tea", price: 300}, {quantity: 2, product_name: "Coca Cola", price: 1500})
print invoice.total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment