Skip to content

Instantly share code, notes, and snippets.

@aoitaku
Last active December 29, 2015 23:59
Show Gist options
  • Save aoitaku/7746383 to your computer and use it in GitHub Desktop.
Save aoitaku/7746383 to your computer and use it in GitHub Desktop.
手続き的な関数やサブルーチンを手続き的に書けるようにするDSL
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