Here is my class:
class Money
def initialize(dollars, quarters, dimes, nickels, pennies)
@coins = [ {:coin => dollars, :price => 100},
{:coin => quarters, :price => 25},
{:coin => dimes, :price => 10},
{:coin => nickels, :price => 5},
{:coin => pennies, :price => 1} ]
end
def count
total = 0.00
coins.each do |coin|
next if coin[:price] == 0
total += coin[:coin] * coin[:price]
end
total / 100
end
end
and I am testing it like this:
money = Money.new( 5, 1, 2, 1, 0 )
puts "$%.2f" % money.count
I am receiving an error:
money.rb:12:in `count': undefined local variable or method `coins' for #<Money:0x2567310> (NameError)
from money.rb:34:in `<main>'
which points to the line coins.each do |coin| and doesn't make sense to me because I thought that, if I prefix a variable with @, I could use it throughout my objects's methods (it will not carry over to a different object).
I got this working using different code that does:
@dollar = dollar
@quarter = quarter
...
for my initialize method (my count method was radically different), but now I am trying to create an array of hash tables so that I could refactor my count method.
Any help will be appreciated.