fork(3) download
  1. class Cal
  2. attr_reader :v1, :v2
  3. attr_writer :v1
  4. @@_history = []
  5. def initialize(v1,v2)
  6. @v1 = v1
  7. @v2 = v2
  8. end
  9. def add()
  10. result = @v1+@v2
  11. @@_history.push("add : #{@v1}+#{@v2}=#{result}")
  12. return result
  13. end
  14. def subtract()
  15. result = @v1-@v2
  16. @@_history.push("subtract : #{@v1}-#{@v2}=#{result}")
  17. return result
  18. end
  19. def setV1(v)
  20. if v.is_a?(Integer)
  21. @v1 = v
  22. end
  23. end
  24. def getV1()
  25. return @v1
  26. end
  27. def Cal.history()
  28. for item in @@_history
  29. p item
  30. end
  31. end
  32. end
  33. class CalMultiply < Cal
  34. def multiply()
  35. result = @v1*@v2
  36. @@_history.push("multipy : #{@v1}*#{@v2}=#{result}")
  37. return result
  38. end
  39. end
  40. class CalDivide < CalMultiply
  41. def divide()
  42. result = @v1/@v2
  43. @@_history.push("divide : #{@v1}/#{@v2}=#{result}")
  44. return result
  45. end
  46. end
  47. c1 = CalMultiply.new(10,10)
  48. p c1.add()
  49. p c1.multiply()
  50. c2 = CalDivide.new(20, 10)
  51. p c2, c2.add()
  52. p c2, c2.multiply()
  53. p c2, c2.divide()
  54. Cal.history()
  55.  
Success #stdin #stdout 0.01s 7420KB
stdin
Standard input is empty
stdout
20
100
#<CalDivide:0x87dec24 @v1=20, @v2=10>
30
#<CalDivide:0x87dec24 @v1=20, @v2=10>
200
#<CalDivide:0x87dec24 @v1=20, @v2=10>
2
"add : 10+10=20"
"multipy : 10*10=100"
"add : 20+10=30"
"multipy : 20*10=200"
"divide : 20/10=2"