extend_your_language.sf 762 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/ruby
  2. class if2(cond1, cond2) {
  3. method then(block) { # both true
  4. if (self.cond1 && self.cond2) {
  5. block.run;
  6. }
  7. return self;
  8. }
  9. method else1(block) { # first true
  10. if (self.cond1 && !self.cond2) {
  11. block.run;
  12. }
  13. return self;
  14. }
  15. method else2(block) { # second true
  16. if (self.cond2 && !self.cond1) {
  17. block.run;
  18. }
  19. return self;
  20. }
  21. method else(block) { # none true
  22. if (!self.cond1 && !self.cond2) {
  23. block.run;
  24. }
  25. return self;
  26. }
  27. }
  28. if2(false, true).then {
  29. say "if2";
  30. }.else1 {
  31. say "else1";
  32. }.else2 {
  33. say "else2"; # <- this gets printed
  34. }.else {
  35. say "else"
  36. }