Pregunta

How does tap method work concurrencywise? Do I have to fear that if I do:

some_object.tap { |o|
  # time-consuming operation 1
}.tap { |o|
  # time-consuming operation 2
}

that, in the present or future, Ruby will try to do these operations concurrently? You know, #tap sounds dangerous.

Is it guaranteed that the #tap block executes in sequence (unless, of course, one does something unusual inside the block)?

¿Fue útil?

Solución

Tap does not execute the block concurrently, but in sequence. So you do not have to worry about concurrency issues because there are no concurrency issues.

Here's the source of tap:

VALUE
rb_obj_tap(VALUE obj)
{
    rb_yield(obj);
    return obj;
}

So you can see that first it calls the block (yield), and only after that it returns the original object. There is no concurrency present.

Otros consejos

You have nothing to worry about.

Ruby execution will be sequential unless you do something to expressly make it parallel (e.g. creating threads).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top