How absurd is this?

Discussion in 'Ruby' started by khaines@enigo.com, Aug 11, 2006.

  1. Guest

    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 Haines
     
    , Aug 11, 2006
    #1
    1. Advertising

  2. On 8/12/06, <> 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
     
    Martin DeMello, Aug 11, 2006
    #2
    1. Advertising

  3. Mike Harris Guest

    Martin DeMello wrote:

    > On 8/12/06, <> 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
    >
    >

    +1
     
    Mike Harris, Aug 11, 2006
    #3
  4. Guest

    On Sat, 12 Aug 2006, Martin DeMello wrote:

    > On 8/12/06, <> 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


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


    Kirk Haines
     
    , Aug 11, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    305
    Default User
    Jul 26, 2006
Loading...

Share This Page