This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Invoking Blocks
CodeExpectedActual
block = { |x| puts x }                 # WRONG
SyntaxError: compile error
...
SyntaxError: compile error
(irb):1: parse error, unexpected '|', expecting '}'
block = { |x| puts x }                 # WRONG
           ^
(irb):1: parse error, unexpected tIDENTIFIER, expecting kDO or '{' or '('
block = { |x| puts x }                 # WRONG
                    ^
	from (irb):1
block = lambda { |x| puts x }          # RIGHT
block.call "Hello World!" 
Hello World!
Hello World!
def my_lambda(&block)
  block
end
b = my_lambda { puts "Hello World My Way!" }
b.call
Hello World My Way!
Hello World My Way!
b.class
Proc Proc
block = Proc.new { |x| puts x }
block = proc { |x| puts x }
block = lambda { |x| puts x }
add_lambda = lambda { |x,y| x + y }
add_lambda.call(4)
ArgumentError: wrong number of arguments (1 for 2)
...
ArgumentError: wrong number of arguments (1 for 2)
	from (irb):13
	from (irb):14
add_lambda.call(4,5,6)
ArgumentError: wrong number of arguments (3 for 2)
...
ArgumentError: wrong number of arguments (3 for 2)
	from (irb):13
	from (irb):15
add_procnew = Proc.new { |x,y| x + y }
add_procnew.call(4)
TypeError: nil can't be coerced into Fixnum
...
TypeError: nil can't be coerced into Fixnum
	from (irb):16:in `+'
	from (irb):16
	from (irb):17
add_procnew.call(4,5,6)
9 9