history_variables.sf 541 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/ruby
  2. #
  3. ## https://rosettacode.org/wiki/History_variables
  4. #
  5. class HistoryVar(v) {
  6. has history = []
  7. has variable = v
  8. method ≔(value) {
  9. history << variable
  10. variable = value
  11. }
  12. method to_s {
  13. "#{variable}"
  14. }
  15. method AUTOLOAD(_, name, *args) {
  16. variable.(name)(args...)
  17. }
  18. }
  19. var foo = HistoryVar(0)
  20. foo ≔ 1
  21. foo ≔ 2
  22. foo ≔ foo+3
  23. foo ≔ 42
  24. say "History: #{foo.history}"
  25. say "Current value: #{foo}"
  26. assert_eq(foo.history, [0,1,2,5])
  27. assert_eq(foo.variable, 42)