Skip to content

Instantly share code, notes, and snippets.

@evanphx
Created April 30, 2015 17:21
Show Gist options
  • Save evanphx/ef17026666dc314bdf85 to your computer and use it in GitHub Desktop.
Save evanphx/ef17026666dc314bdf85 to your computer and use it in GitHub Desktop.
Ruby "type" checking
def foo(a : String)
# the method prologue is injected with
raise TypeError unless String === a
end
# Allows for other interesting ones, like:
def use_as_number(a : /\d+/)
end
# and more interesting
class RespondTo
def initialize(method)
@method = method
end
def ===(o)
o.respond_to? @method
end
end
ToS = RespondTo.new(:to_s)
def stringy(s : ToS)
# same prologue as before, now doing a respond_to check.
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment