Skip to content

Instantly share code, notes, and snippets.

@pasberth
Created August 27, 2011 15:53
Show Gist options
  • Select an option

  • Save pasberth/1175529 to your computer and use it in GitHub Desktop.

Select an option

Save pasberth/1175529 to your computer and use it in GitHub Desktop.
Python でこういうことをしたいっていう願望
# Python でもこんな感じのものを定義して
# ["<a>" + e + "</a>" for e in r(['root1', ['branch0', 'branch1'], 'root2', ['branches'] ])]
# って書きたい
class Array
def r &blk
[].tap { |res|
each { |e|
if e.is_a? Array
res << e.r(&blk)
else
res << blk.call(e)
end
}
}
end
end
if __FILE__ == $PROGRAM_NAME
require "test/unit"
class TestR < Test::Unit::TestCase
def test_r
assert_equal ['root1', ['branch0', 'branch1'], 'root2', ['branches'] ].r { |e|
'<a>' + e + '</a>'
}, ["<a>root1</a>", ["<a>branch0</a>", "<a>branch1</a>"], "<a>root2</a>", ["<a>branches</a>"]]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment