Last active
December 29, 2015 23:59
-
-
Save aoitaku/7746383 to your computer and use it in GitHub Desktop.
手続き的な関数やサブルーチンを手続き的に書けるようにするDSL
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 method_missing(symbol, *argument) | |
if argument.size == 1 && Proc === argument.first | |
[symbol, argument.first] | |
else | |
super | |
end | |
end | |
def const_missing(symbol, *argument) | |
if argument.size == 1 && Proc === argument.first | |
[symbol, argument.first] | |
else | |
super | |
end | |
end | |
def sub(pair) | |
symbol, lambda = pair | |
Object.class_eval { const_set(symbol, lambda) } | |
end | |
def func(arg) | |
case arg | |
when Proc | |
arg | |
when Array | |
symbol, lambda = arg | |
define_method(symbol, lambda) | |
end | |
end | |
def call(subroutine, *arguments) | |
subroutine.call *arguments | |
end | |
sub Subroutine -> argument { | |
p argument | |
} | |
call Subroutine, 'Hello' | |
func function -> argument { | |
p argument | |
} | |
function 'World' | |
anonym = func -> argument { | |
p argument | |
} | |
call anonym, 'Hello, World.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment