Skip to content

Instantly share code, notes, and snippets.

@dexterous
Created February 28, 2011 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dexterous/847115 to your computer and use it in GitHub Desktop.
Save dexterous/847115 to your computer and use it in GitHub Desktop.
various use cases for ruby blocks
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
@dexterous
Copy link
Author

dexter@dexters-lab01:~/repo/847115$ /opt/ruby/1.8.7-p330/bin/ruby proc_expectations.rb 2>err && cat err
Expectations ........
Finished in 0.00744 seconds

Success: 8 fulfilled
proc_expectations.rb:7: warning: multiple values for a block parameter (2 for 1) from proc_expectations.rb:7
proc_expectations.rb:11: warning: multiple values for a block parameter (2 for 1) from proc_expectations.rb:11

dexter@dexters-lab01:~/repo/847115$ /opt/ruby/1.9.2-p136/bin/ruby proc_expectations.rb 2>err && cat err
Expectations .F..F.F.
Finished in 0.00706 seconds

Failure: 3 failed, 0 errors, 5 fulfilled

--Failures--
file <proc_expectations.rb>
line <7>
expected: <[1, 2]> got: <1>

file <proc_expectations.rb>
line <11>
expected: <[1, 2]> got: <1>

file <proc_expectations.rb>
line <20>
expected block to be == :method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment