class Calc
attr_reader :sum
def initialize
@operation = nil
@sum = 0
end
def plus
@operation = ->(a,b){ a + b }
self
end
def minus
@operation = ->(a,b){ a - b }
self
end
def times
@operation = ->(a,b){ a * b }
self
end
def divided_by
@operation = ->(a,b){ a.fdiv(b) }
self
end
def one
operate(1)
self
end
def two
operate(2)
self
end
def three
operate(3)
self
end
def to_s
@sum
end
def inspect
@sum
end
private
def operate(value)
if @operation
@sum = @operation.(@sum, value)
@operation = nil
else
@sum = value
end
end
end
p Calc.new.one.plus.two
p Calc.new.one.minus.two.divided_by.three
Y2xhc3MgQ2FsYwoJYXR0cl9yZWFkZXIgOnN1bQoJCiAgZGVmIGluaXRpYWxpemUKICAJQG9wZXJhdGlvbiA9IG5pbAogIAlAc3VtID0gMAogIGVuZAogIAogIGRlZiBwbHVzCiAgCUBvcGVyYXRpb24gPSAtPihhLGIpeyBhICsgYiB9CiAgCXNlbGYKICBlbmQKICAKICBkZWYgbWludXMKICAJQG9wZXJhdGlvbiA9IC0+KGEsYil7IGEgLSBiIH0KICAJc2VsZgogIGVuZAogIAogIGRlZiB0aW1lcwogIAlAb3BlcmF0aW9uID0gLT4oYSxiKXsgYSAqIGIgfQogIAlzZWxmCiAgZW5kCiAgCiAgZGVmIGRpdmlkZWRfYnkKICAJQG9wZXJhdGlvbiA9IC0+KGEsYil7IGEuZmRpdihiKSB9CiAgCXNlbGYKICBlbmQKICAKICBkZWYgb25lCiAgCW9wZXJhdGUoMSkKICAJc2VsZgogIGVuZAogIAogIGRlZiB0d28KICAJb3BlcmF0ZSgyKQogIAlzZWxmCiAgZW5kCiAgCiAgZGVmIHRocmVlCiAgCW9wZXJhdGUoMykKICAJc2VsZgogIGVuZAogIAogIGRlZiB0b19zCiAgCUBzdW0KICBlbmQKICAKICBkZWYgaW5zcGVjdAogIAlAc3VtCiAgZW5kCiAgCiAgcHJpdmF0ZQogIAogIGRlZiBvcGVyYXRlKHZhbHVlKQogIAlpZiBAb3BlcmF0aW9uCiAgICAgIEBzdW0gPSBAb3BlcmF0aW9uLihAc3VtLCB2YWx1ZSkKICAJICBAb3BlcmF0aW9uID0gbmlsCiAgCWVsc2UKICAJICBAc3VtID0gdmFsdWUKICAJIGVuZAogIGVuZAplbmQKCnAgQ2FsYy5uZXcub25lLnBsdXMudHdvCnAgQ2FsYy5uZXcub25lLm1pbnVzLnR3by5kaXZpZGVkX2J5LnRocmVl