tags:

views:

104

answers:

3

I am sorry to bring out a separate question from here, but it really confused me these days. Here is the definition of add_mapping method:

def add_mapping(regexp, &proc)
  @test_mappings << [regexp, proc]
end

How could proc here get executed and return result without using call method?

Thanks

+1  A: 

Yep, according to the documentation, [] is a synonym for Proc#call.

geowa4
@George, if the [] is the same as call to proc, what @test_mappings << [regexp, proc] should means? I still don't understand.
eric2323223
Eric: the result is both stored in the @test_mappings object/array, and returned simultaneously. (If I understand the code right.)
The Wicked Flea
In standard ruby the proc does not get called. Putting a proc in an array is not the same as calling the :[] method.
krusty.ar
+3  A: 

Eric, what add_mapping does is just adding a regular expresion + proc tuple to an array called @test_mappings.

add_mappings does not execute the proc. I don't know how ZenTest works but it should be executing the procs after reading all add_mapping calls.

Check out the code for ZenTest and look out for @test_mappings, that could reveal you where (and when) the proc is executed.

andrerobot
+1  A: 

A proc can be executed by being passed as a block to a yielding method, or by being called manually (as you mention).

a_proc = proc { puts "The proc" }

def i_am_yielding
  yield
end

a_proc.call
# => "The proc"
i_am_yielding { puts "A block" }
# => "A block"
i_am_yielding(&a_proc)
# => "The proc"

Perhaps there are more ways, but I can't think of any. I am not sure what it means in your particular case, though. That proc is inside an array, which is added to another array (i presume), and not being referenced to at all anywhere else, so no procs will get called in that short snippet you paste here.

August Lilleaas