class Environment attr_reader :parent, :defaults def initialize(parent=nil, defaults={}) @parent = parent @defaults = defaults end def define(symbol, value) defaults[symbol] = value end def defined?(symbol) defaults.has_key?(symbol) || (parent && parent.defined?(symbol)) end def lookup(symbol) defaults[symbol] || (parent && parent.lookup(symbol)) || raise("#{symbol} is undefined") end def set(symbol, value) if defaults.has_key?(symbol) defaults[symbol] = value elsif parent.nil? raise("#{symbol} was undefined") else parent.set(symbol, value) end end end