How absurd is this?

K

khaines

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
 
M

Martin DeMello

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
 
M

Mike Harris

Martin said:
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
 
K

khaines

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top