Reputation: 19
In an application of mine, I have some code that looks like this:
if is_translation?
@booking.enable_dirty_associations do
booking_update
end
else
booking_update
end
I would like it instead to look like this:
is_translation? ? @booking.enable_dirty_associations : func do
booking_update
end
Where func
is the method that takes the block and just executes it.
Is there a built in Ruby method, or perhaps a combinator function that does this?
Upvotes: 1
Views: 345
Reputation: 3253
It would be pretty easy to write func() for yourself:
def func
yield
end
Unfortunately, however, your idea won't work, the block will only apply to func
, not the first call. The only way I can think of to get close to what you want is to define the block as a proc and pass it manually:
block = Proc.new { booking_update }
is_translation? ? @booking.enable_dirty_associations(&block) : block.call
This does have the advantage of not needing the func()
method.
Upvotes: 1