Created
July 21, 2013 16:07
-
-
Save plonk/6048962 to your computer and use it in GitHub Desktop.
無名関数を再帰させる構造
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def make_recur(&def_block) | |
| delegate = proc { |*args| | |
| user_proc = def_block.call(delegate) | |
| user_proc.call(*args) | |
| } | |
| end | |
| p make_recur { |myproc| | |
| proc { |n| | |
| (n == 1) ? 1 : n * myproc.call(n-1) | |
| } | |
| }.call(5) | |
| # OUTPUT: 120 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment