Skip to content

Instantly share code, notes, and snippets.

@jjb
Last active December 14, 2015 00:29
Show Gist options
  • Save jjb/4999475 to your computer and use it in GitHub Desktop.
Save jjb/4999475 to your computer and use it in GitHub Desktop.
class Object
def self.typecheck(object)
unless self == object.class
raise ArgumentError,
"expected type: #{self}. given: #{object.class}."
end
end
end
class Foo; end
class Bar; end
def my_method(arg1, arg2)
Foo.typecheck arg1
Bar.typecheck arg2
puts "The method finished!"
end
puts "1..."
my_method(Foo.new, Bar.new)
puts "2..."
my_method("hi.", Bar.new)
1...
The method finished!
2...
driver.rb:4:in `typecheck': expected type: Foo. given: String. (ArgumentError)
from driver.rb:14:in `my_method'
from driver.rb:23:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment