Proc in ruby 1.8.6 and 1.8.7


#1

In Ruby 1.8.7 it is possible to do such thing:

pr = Proc.new do |arg, &block|
block.call
end

pr.call(123) do puts “xyz” end
xyz
=> nil

When calling proc another block can be given as parameter.

Is there any way to do it in Ruby 1.8.6 ?

Not working:

irb(main):001:0> aba = Proc.new do |arg, &block|
irb(main):002:1* end
SyntaxError: compile error
(irb):1: syntax error, unexpected tAMPER, expecting ‘|’
aba = Proc.new do |arg, &block|
^
from (irb):2

==

Not working:

irb(main):003:0> pr = Proc.new do |arg, block|
irb(main):004:1* block.call
irb(main):005:1> end
=> #Proc:0xb7d2d630@:3(irb)
irb(main):006:0> pr.call(123) do puts “xyz” end
NoMethodError: undefined method call' for nil:NilClass from (irb):4 from (irb):6:incall’
from (irb):6
irb(main):007:0>

==

Working, but i want to avoid this solution:

irb(main):007:0> pr.call 123, Proc.new{puts “xyz”}
xyz
=> nil
irb(main):008:0>

I hope you people know some cool workaround.

My application is currently running on Ruby 1.8.7 but i would like to
use REE instead and right now this is the only issue that stops me from
it.


#2

Robert Pankowecki wrote:

Working, but i want to avoid this solution:

irb(main):007:0> pr.call 123, Proc.new{puts “xyz”}

Yes, that’s what you need - to pass the block as an explicit argument.