here 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 |coin| next if coin[:price] == 0 total += coin[:coin] * coin[:price] end total / 100 end end and testing this:
money = money.new( 5, 1, 2, 1, 0 ) puts "$%.2f" % money.count i receiving error:
money.rb:12:in `count': undefined local variable or method `coins' #<money:0x2567310> (nameerror) money.rb:34:in `<main>' which points line coins.each |coin| , doesn't make sense me because thought that, if prefix variable @, use throughout objects's methods (it not carry on different object).
i got working using different code does:
@dollar = dollar @quarter = quarter ... for initialize method (my count method radically different), trying create array of hash tables refactor count method.
any appreciated.
in count() method refer coins @coins. otherwise you've created variable available method in , not referencing instance variable created in initialize()
so reads:
def count total = 0.00 @coins.each |coin| next if coin[:price] == 0 total += coin[:coin] * coin[:price] end total / 100 end
Comments
Post a Comment