Guest User

Untitled

a guest
Jan 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. # Python でもこんな感じのものを定義して
  2. # ["<a>" + e + "</a>" for e in r(['root1', ['branch0', 'branch1'], 'root2', ['branches'] ])]
  3. # って書きたい
  4. class Array
  5.  
  6. def r &blk
  7. [].tap { |res|
  8. each { |e|
  9. if e.is_a? Array
  10. res << e.r(&blk)
  11. else
  12. res << blk.call(e)
  13. end
  14. }
  15. }
  16. end
  17.  
  18. end
  19.  
  20. if __FILE__ == $PROGRAM_NAME
  21. require "test/unit"
  22. class TestR < Test::Unit::TestCase
  23. def test_r
  24. assert_equal ['root1', ['branch0', 'branch1'], 'root2', ['branches'] ].r { |e|
  25. '<a>' + e + '</a>'
  26. }, ["<a>root1</a>", ["<a>branch0</a>", "<a>branch1</a>"], "<a>root2</a>", ["<a>branches</a>"]]
  27. end
  28. end
  29. end
Add Comment
Please, Sign In to add comment