# How absurd is this?

**URL:** <https://www.ruby-forum.com/t/how-absurd-is-this/102431>\
**Category:** Ruby\
**Created:** [August 15, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/how-absurd-is-this/102431 "2006-08-15T14:17:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 15, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/how-absurd-is-this/102431/1 "2006-08-15T14:17:16Z")

</div>

Imagine you have in a method a block. Sometimes you will want that  
block  
to be executed via a mutex.synchronize() call. And sometimes you don’t  
want to synchronize.

def y  
yield  
end

def method\_with\_a\_block  
(dont\_lock\_here ? method(:y) : @mutex.method(:synchronize)).call do  
puts “This block may or may not be synchronized, depending on  
whether  
dont\_lock\_here() returned true or false.”  
end  
end

Is that a stupid way to get that effect?

The only other alternative that occurs to me is to make the block a  
separate method.

def i\_was\_a\_block  
puts “This code may or may not be syncronized, depending on whether  
dont\_lock\_here() in method\_with\_a\_block returned true or false.”  
end

def method\_with\_a\_block  
if dont\_lock\_here  
i\_was\_a\_block  
else  
@mutex.synchronize {i\_was\_a\_block}  
end  
end

What do you all think?

Kirk H.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 15, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/how-absurd-is-this/102431/2 "2006-08-15T14:17:17Z")

</div>

On 8/12/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Imagine you have in a method a block. Sometimes you will want that block  
> to be executed via a mutex.synchronize() call. And sometimes you don’t  
> want to synchronize.

I’d add a Mutex#synchronize\_unless

def method\_with\_a\_block  
@mutex.synchronize\_unless(dont\_lock\_here()) do  
puts “This block may or may not be synchronized, depending on  
whether  
dont\_lock\_here() returned true or false.”  
end  
end

martin

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 15, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/how-absurd-is-this/102431/3 "2006-08-15T14:17:18Z")

</div>

Martin DeMello wrote:

> def method\_with\_a\_block  
> @mutex.synchronize\_unless(dont\_lock\_here()) do  
> puts “This block may or may not be synchronized, depending on  
> whether  
> dont\_lock\_here() returned true or false.”  
> end  
> end
> 
> martin

+1

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 15, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/how-absurd-is-this/102431/4 "2006-08-15T14:17:18Z")

</div>

On Sat, 12 Aug 2006, Martin DeMello wrote:

> dont\_lock\_here() returned true or false."  
> end  
> end

That’s perfect. And obvious. 🙂 Thanks much for helping me see it.

Kirk H.
