Created
February 28, 2011 09:12
-
-
Save dexterous/847115 to your computer and use it in GitHub Desktop.
various use cases for ruby blocks
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
require 'rubygems' | |
require 'expectations' | |
Expectations do | |
expect(1){ proc{ |a| a }.call(1) } | |
expect([1, 2]){ proc{ |a| a }.call(1, 2) } | |
expect(1){ proc{ |a, b| a }.call(1, 2) } | |
expect(1){ Proc.new{ |a| a }.call(1) } | |
expect([1, 2]){ Proc.new{ |a| a }.call(1, 2) } | |
expect(1){ Proc.new{ |a, b| a }.call(1, 2) } | |
def foo | |
x = proc { return :block } | |
x.call() | |
return :method | |
end | |
expect foo().to.be == :method | |
def bar | |
x = Proc.new { return :block } | |
x.call() | |
return :method | |
end | |
expect bar().to.be == :block | |
=begin | |
def boo | |
yield :block | |
return :method | |
end | |
# does not compile as return not allowed in implicit block | |
expect boo{ |a| return a }.to.be == :method | |
def baz &block | |
block.call(:block) | |
return :method | |
end | |
# not even when block param is explicitly declared in method | |
expect baz{ |a| return a }.to.be == :block | |
=end | |
end |
Author
dexterous
commented
Feb 28, 2011
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment