Proc in ruby 1.8.6 and 1.8.7

  • Thread starter Robert Pankowecki
  • Start date
R

Robert Pankowecki

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@(irb):3>
irb(main):006:0> pr.call(123) do puts "xyz" end
NoMethodError: undefined method `call' for nil:NilClass
from (irb):4
from (irb):6:in `call'
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.
 
B

Brian Candler

Robert said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top