Question

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)?

Was it helpful?

Solution

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.

OTHER TIPS

You have nothing to worry about.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top