On Sun, Apr 26, 2009 at 6:48 PM, James Gray <james / grayproductions.net> wrote:
> I hate to be the guy to start another { } vs. do end thread, but I have
> some questions I would love to hear opinions on.
>
> I use to just use the rule of { } for one-liners and do end for the
> longer stuff.  ɧ     > times when I care about the return value and do end for the times the
> block is for side effects.      > but sometimes I have trouble deciding which to use.

Well, I went back to what I wrote over a year ago on this
"controversy" and I think it stands up pretty well.

http://talklikeaduck.denhaven2.com/2007/10/02/ruby-blocks-do-or-brace

I for one, eschew dogmatism. When I went back and re-read that article
I was pleased to see that I was far from dogmatic.  I talk about
leaning or tilting one way or the other.  This is more about
aesthetics than laws of nature or rocket surgery.

> Let me give two examples that have made me stop and think.
>
> First, tap() is Ruby 1.9 is a little tricky.     
> value, but not the return value of the block, so which strategy should I
> use?         
> practice:
>
>   
>   
>  

Here I'd go with braces, because as I say in that article, that

    end.whatever

just grates with me.  On the other hand if it was

var = arr.sort.tap.do | sorted|
    #.....
 end

I would probably lean more towards do/end, but then again, if the
block was a one-liner like yours then I might well 'break the tie"
with the one-vs-multi line idea

var = arr.sort.tap {|sorted| p sorted}

In fact I'd probably be inclined to write your example on one line

arr.sort.tap {|sorted| p sorted}.whatever

> Another example is with a transaction() method for a database.  > such a tool, I often end up with calls where I care about both the side
> effects (transactional behavior) and the return value:
>
>  >  ۺ >  ۺ>  

This one looks a little odd to my eye just because most of the db
transaction delimiting methods I've seen don't return the value of the
block, they are concerned simply with marking a transaction boundary.


-- 
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale