Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Methods returning symbols is nice
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
You can’t perform that action at this time.