Last active
August 29, 2015 14:13
-
-
Save burlesona/5426c1f3a4b9b235f22b to your computer and use it in GitHub Desktop.
Methods returning symbols is nice
This file contains hidden or 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
foo = method def foo | |
def a(num) | |
3 * num.to_i | |
end | |
n = yield if block_given? | |
a(n || 3) | |
rescue | |
"oops!" | |
end | |
def foo.bar(num) | |
a(num) | |
end | |
require 'minitest/autorun' | |
describe "foo" do | |
it "should be a method object" do | |
foo.class.must_equal Method | |
end | |
it "should also be a method (if parens are used)" do | |
foo().must_equal 9 | |
end | |
it "should return 9 when called with no block" do | |
foo.call.must_equal 9 | |
end | |
it "should return block value * 3 when called with a block" do | |
foo.call{2}.must_equal 6 | |
end | |
it "should be callable with block with parens too" do | |
foo(){2}.must_equal 6 | |
end | |
it "should rescue when given a naughty block" do | |
foo.call{ raise "blam!" }.must_equal "oops!" | |
end | |
it "should expose foo's a method as bar" do | |
foo.bar(5).must_equal 15 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment