class RubyChangeMaker::Change

Constants

BILLS

google: common usd denominations

COINS

Public Class Methods

new() click to toggle source
# File lib/rubychangemaker/rubychangemaker.rb, line 14
def initialize()
end

Public Instance Methods

make_change( amount ) click to toggle source

split the denomination entered into bills and coins then, determine change using least amount of bills and coins

# File lib/rubychangemaker/rubychangemaker.rb, line 19
def make_change( amount )
  #REMOVED IN REFACTOR:  coins = changer( pull_cents( f ), COINS )
  #REMOVED IN REFACTOR:  bills = changer( pull_bills( f ), BILLS )
  coins = changer( pull_currency( amount ){ | s, idx | currency = s[ idx + 1..-1 ] }, COINS )
  bills = changer( pull_currency( amount ){ | s, idx | currency = s[ 0...idx     ] }, BILLS )
  [ bills, coins ]
end

Private Instance Methods

changer( amount, arr ) click to toggle source
# File lib/rubychangemaker/rubychangemaker.rb, line 37
def changer ( amount, arr )
  denominations = Hash.new( 0 )
  
  #run through each standard currency denomination
  for current_denomination in arr
  
    #whole number of times denomination goes into start value
    how_many = Integer( amount / current_denomination )
  
    #new start value is remainder from above
    amount %= current_denomination
  
    #assign currency denomination as key, number of even divisions as value
    denominations[ current_denomination ] = how_many
  end
  change = Hash.new( 0 )
      denominations.each { | denomination, quantity | if quantity >= 1 then change[ denomination ] = quantity end }
      
  change
end
pull_currency( amount, &block ) click to toggle source

refactor method to reduce code duplication

# File lib/rubychangemaker/rubychangemaker.rb, line 30
def pull_currency( amount, &block )
  amount_str          = Float( amount ).to_s
  idx                 = amount_str.index( "." )
  currency            = block.call( amount_str, idx )
  currency.to_i
end