Skip to content

Instantly share code, notes, and snippets.

@k-tsj
Created December 28, 2013 09:50
Show Gist options
  • Save k-tsj/8157847 to your computer and use it in GitHub Desktop.
Save k-tsj/8157847 to your computer and use it in GitHub Desktop.
Type(pattern) annotations for Ruby
require 'pattern-match'
class Module
def check(pattern, mid)
prepend Module.new {
define_method(mid) do |*args|
ret = super(*args)
match(ret) do
with(Kernel.eval(pattern, Kernel.binding)) { ret }
end
end
}
end
end
class C
check '_[Integer]',
def m
[0]
end
end
C.new.m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment