irbで2回以上loadすると失敗する
rubyのirbでrubyのコードを2回以上loadすると失敗します。
なぜなのでしょうか?
pythonだと
import mymodule
import importlib
importlib.reload(mymodule) # 変更が反映される
とするとモジュールの変更が反映されますが
rubyだとどうするのでしょうか?
>> load "./big_step.rb" #2回目
TypeError: superclass mismatch for class Number
big_step.rb
class Number < Struct.new(:value)
def to_s
return value.to_s
end
def evaluate(environment)
self
end
def inspect
return "<<#{self}>>"
end
end
class Multiply < Struct.new(:left, :right)
def to_s
return "#{left} * #{right}"
end
def inspect
return "<<#{self}>>"
end
def evaluate(environment)
Number.new(left.evaluate(environment).value * right.environment(environment).value)
end
end
class Add < Struct.new(:left, :right)
def to_s
return "#{left} + #{right}"
end
def inspect
return "<<#{self}>>"
end
def evaluate(environment)
Number.new(left.evaluate(environment).value + right.evaluate(environment).value)
end
end
class Boolean < Struct.new(:value)
def to_s
value.to_s
end
def inspect
"<<#{self}>>"
end
def evaluate(environment)
self
end
end
class LessThan < Struct.new(:left, :right)
def to_s
"#{left} < #{right}"
end
def inspect
"<<#{self}>>"
end
end
class Variable < Struct.new(:name)
def to_s
name.to_s
end
def inspect
"<<#{self}>>"
end
def evaluate(environment)
environment[name]
end
end
class DoNothing
def to_s
"do-noting"
end
def inspect
"<<#{self}>>"
end
def ==(other_statement)
other_statement.instance_of?(DoNothing)
end
end
class Assign < Struct.new(:name, :expression)
def to_s
"#{name} = #{expression}"
end
def inspect
"<<#{self}>>"
end
def evaluate(environment)
environment.merge({name => expression.evaluate(environment)})
end
end
class If < Struct.new(:condition, :true_exe, :false_exe)
def to_s
"if (#{condition}) { #{true_exe} } else { #{false_exe} }"
end
def inspect
"<<#{self}>>"
end
def evaluate(environment)
case condition.evaluate(environment)
when Boolean.new(true)
true_exe.evaluate(environment)
when Boolean.new(false)
false_exe.evaluate(environment)
end
end
end
class Sequence < Struct.new(:first, :second)
def to_s
"#{first}; #{second}"
end
def inspect
"<<#{self}>>"
end
def evaluate(environment)
second.evaluate(first.evaluate(environment))
end
end
class While < Struct.new(:condition, :body)
def to_s
"while (#{condition}) { #{body} }"
end
def inspect
"<<#{self}>>"
end
def evaluate(environment)
case condition.evaluate(environment)
when Boolean.new(true)
evaluate(body.evaluate(environment))
when Boolean.new(false)
environment
end
end
end