Last active
August 29, 2015 14:04
-
-
Save banister/acec9b4688cbb7575106 to your computer and use it in GitHub Desktop.
This file contains 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
[1] pry(main)> def returns(type) | |
[1] pry(main)* ->(method_name) { | |
[1] pry(main)* old_method = instance_method(method_name) | |
[1] pry(main)* define_method(method_name) do |*args, &block| | |
[1] pry(main)* return_value = old_method.bind(self).call(*args, &block) | |
[1] pry(main)* unless return_value.class <= type | |
[1] pry(main)* raise TypeError, "Return value is not of type #{type}!" | |
[1] pry(main)* end | |
[1] pry(main)* return_value | |
[1] pry(main)* end | |
[1] pry(main)* } | |
[1] pry(main)* end | |
=> :returns | |
[2] pry(main)> class Hello | |
[2] pry(main)* returns(Integer).call def hello | |
[2] pry(main)* 5 | |
[2] pry(main)* end | |
[2] pry(main)* | |
[2] pry(main)* returns(String).call def goodbye | |
[2] pry(main)* 5 | |
[2] pry(main)* end | |
[2] pry(main)* end | |
=> :goodbye | |
[3] pry(main)> Hello.new.hello | |
=> 5 | |
[4] pry(main)> Hello.new.goodbye | |
TypeError: Return value is not of type String! | |
from (pry):7:in `block (2 levels) in returns' | |
[5] pry(main)> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment