This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Discounter | |
def discount(*skus) | |
expensive_calc(skus) | |
end | |
private | |
def expensive_calc(*skus) | |
puts "in expensive calc for #{skus}" | |
skus.inject{|m,n| m + n} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Discounter | |
def discount(*skus) | |
@memory ||= {} | |
@memory[skus] = expensive_calc(skus) unless @memory.has_key?(skus) | |
@memory[skus] | |
end | |
private | |
def expensive_calc(*skus) | |
puts "in expensive calc for #{skus}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Discounter | |
def discount(*skus) | |
expensive_calc(skus) | |
end | |
private | |
def expensive_calc(*skus) | |
puts "in expensive calc for #{skus}" | |
skus.inject{|m,n| m + n} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Discounter | |
def discount(*skus) | |
expensive_calc(skus) | |
end | |
private | |
def expensive_calc(*skus) | |
puts "in expensive calc for #{skus}" | |
skus.inject{|m,n| m + n} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Discounter | |
def discount(*skus) | |
expensive_calc(skus) | |
end | |
private | |
def expensive_calc(*skus) | |
puts "in expensive calc for #{skus}" | |
skus.inject{|m,n| m + n} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Discounter | |
def discount(*skus) | |
expensive_calc(skus) | |
end | |
private | |
def expensive_calc(*skus) | |
puts "in expensive calc for #{skus}" | |
skus.inject{|m,n| m + n} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Memoize | |
def remember(method) | |
orig_method_name = "_original_#{method.to_s}_" | |
alias_method orig_method_name.to_sym, method | |
define_method(method) do |*args| | |
@memory ||= {} | |
@memory[args] = send(orig_method_name,args) unless @memory.has_key?(args) | |
@memory[args] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Memoize | |
def remember(method) | |
original_method = instance_method(method) | |
define_method(method) do |*args| | |
@memory ||= {} | |
bound_method = original_method.bind(self) | |
@memory[args] = bound_method.call(args) unless @memory.has_key?(args) | |
@memory[args] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Memoize | |
def remember(method, &block) | |
define_method(method, block) | |
original_method = instance_method(method) | |
define_method(method) do |*args| | |
@memory ||= {} | |
bound_method = original_method.bind(self) | |
@memory[args] = bound_method.call(args) unless @memory.has_key?(args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ShippingOption | |
def self.children | |
@children ||= [] | |
end | |
def self.inherited(child_class) | |
children << child_class | |
end | |
def self.for(weight, international) |