fork download
  1. # Swift's closure
  2. # https://d...content-available-to-author-only...e.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html
  3.  
  4.  
  5. require 'continuation' # Need for Ruby 1.9 or later
  6.  
  7.  
  8. DigitNames = {
  9. 0 => "Zero", 1 => "One", 2 => "Two", 3 => "Three", 4 => "Four",
  10. 5 => "Five", 6 => "Six", 7 => "Seven", 8 => "Eight", 9 => "Nine"
  11. }
  12. Numbers = [16, 58, 510]
  13.  
  14.  
  15. strings = Numbers.map { |number|
  16. callcc { |continuation|
  17. loop.inject([number, '']) { |(num, output), _|
  18. if num > 0
  19. [num / 10, DigitNames[num % 10] + output]
  20. else
  21. continuation.call output
  22. end
  23. }
  24. }
  25. }
  26.  
  27.  
  28. for s in strings
  29. puts s
  30. end
Success #stdin #stdout 0.02s 7488KB
stdin
Standard input is empty
stdout
OneSix
FiveEight
FiveOneZero