# How absurd is this?

**URL:** <https://rubytalk.org/t/how-absurd-is-this/29880>\
**Category:** ruby-talk\
**Created:** [11 August 2006 19:18 UTC](https://rubytalk.org/t/how-absurd-is-this/29880 "2006-08-11T19:18:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kirk\_Haines](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Kirk\_Haines](https://rubytalk.org/u/Kirk_Haines)\
**Post date:** [11 August 2006 19:18 UTC](https://rubytalk.org/t/how-absurd-is-this/29880/1 "2006-08-11T19:18:22Z")

</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  
&nbsp;&nbsp;&nbsp;yield  
end

def method\_with\_a\_block  
&nbsp;&nbsp;&nbsp;(dont\_lock\_here ? method(:y) : @mutex.method(:synchronize)).call do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "This block may or may not be synchronized, depending on whether dont\_lock\_here() returned true or false."  
&nbsp;&nbsp;&nbsp;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  
&nbsp;&nbsp;&nbsp;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  
&nbsp;&nbsp;&nbsp;if dont\_lock\_here  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i\_was\_a\_block  
&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@mutex.synchronize {i\_was\_a\_block}  
&nbsp;&nbsp;&nbsp;end  
end

What do you all think?

Kirk Haines

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [11 August 2006 19:27 UTC](https://rubytalk.org/t/how-absurd-is-this/29880/2 "2006-08-11T19:27:40Z")

</div>

I'd add a Mutex#synchronize\_unless

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

martin

> **···**
>
> On 8/12/06, khaines@enigo.com \<khaines@enigo.com\> 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.

---

<div class="post-metadata">

**Author:** ![Mike\_Harris](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@Mike\_Harris](https://rubytalk.org/u/Mike_Harris)\
**Post date:** [11 August 2006 19:37 UTC](https://rubytalk.org/t/how-absurd-is-this/29880/3 "2006-08-11T19:37:13Z")

</div>

Martin DeMello wrote:

> **···**
>
> > On 8/12/06, khaines@enigo.com \<khaines@enigo.com\> 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  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "This block may or may not be synchronized, depending on whether  
> > dont\_lock\_here() returned true or false."  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > martin
> 
> +1

---

<div class="post-metadata">

**Author:** ![Kirk\_Haines](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Kirk\_Haines](https://rubytalk.org/u/Kirk_Haines)\
**Post date:** [11 August 2006 19:45 UTC](https://rubytalk.org/t/how-absurd-is-this/29880/4 "2006-08-11T19:45:12Z")

</div>

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

Kirk Haines

> **···**
>
> On Sat, 12 Aug 2006, Martin DeMello wrote:
> 
> > On 8/12/06, khaines@enigo.com \<khaines@enigo.com\> 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  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "This block may or may not be synchronized, depending on whether  
> > dont\_lock\_here() returned true or false."  
> > &nbsp;&nbsp;end  
> > end
