class Cal
attr_reader :v1, :v2
attr_writer :v1
@@_history = []
def initialize(v1,v2)
@v1 = v1
@v2 = v2
end
def add()
result = @v1+@v2
@@_history.push("add : #{@v1}+#{@v2}=#{result}")
return result
end
def subtract()
result = @v1-@v2
@@_history.push("subtract : #{@v1}-#{@v2}=#{result}")
return result
end
def setV1(v)
if v.is_a?(Integer)
@v1 = v
end
end
def getV1()
return @v1
end
def Cal.history()
for item in @@_history
p item
end
end
end
class CalMultiply < Cal
def multiply()
result = @v1*@v2
@@_history.push("multipy : #{@v1}*#{@v2}=#{result}")
return result
end
end
class CalDivide < CalMultiply
def divide()
result = @v1/@v2
@@_history.push("divide : #{@v1}/#{@v2}=#{result}")
return result
end
end
c1 = CalMultiply.new(10,10)
p c1.add()
p c1.multiply()
c2 = CalDivide.new(20, 10)
p c2, c2.add()
p c2, c2.multiply()
p c2, c2.divide()
Cal.history()
Y2xhc3MgQ2FsCiAgYXR0cl9yZWFkZXIgOnYxLCA6djIKICBhdHRyX3dyaXRlciA6djEKICBAQF9oaXN0b3J5ID0gW10KICBkZWYgaW5pdGlhbGl6ZSh2MSx2MikKICAgIEB2MSA9IHYxCiAgICBAdjIgPSB2MgogIGVuZAogIGRlZiBhZGQoKQogICAgcmVzdWx0ID0gQHYxK0B2MgogICAgQEBfaGlzdG9yeS5wdXNoKCJhZGQgOiAje0B2MX0rI3tAdjJ9PSN7cmVzdWx0fSIpCiAgICByZXR1cm4gcmVzdWx0CiAgZW5kCiAgZGVmIHN1YnRyYWN0KCkKICAgIHJlc3VsdCA9IEB2MS1AdjIKICAgIEBAX2hpc3RvcnkucHVzaCgic3VidHJhY3QgOiAje0B2MX0tI3tAdjJ9PSN7cmVzdWx0fSIpCiAgICByZXR1cm4gcmVzdWx0CiAgZW5kCiAgZGVmIHNldFYxKHYpCiAgICBpZiB2LmlzX2E/KEludGVnZXIpCiAgICAgIEB2MSA9IHYKICAgIGVuZAogIGVuZAogIGRlZiBnZXRWMSgpCiAgICByZXR1cm4gQH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