On Sun, Apr 26, 2009 at 11: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.
>
> 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:
>
>   
>   
>  
> 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:
>
>  >  ۺ >  ۺ>  
>
> Any thoughts on how edge cases like this mesh with the block strategy?
>
> James Edward Gray II
>
>
>

I prefer {} when using blocks in a functional style, do..end when
using statements but I'm with Rick on the 'foolish consistency'.

In your first example, it would appear that you ~are~ concerned with
the return value of the block as you call #whatever on it, so I'd use:

  arr.sort.tap { |sorted|
    p sorted
  }.whatever
assuming that the 'p sorted' stands for a multiline statement. In 1.9
you can even do:

  arr
    .sort
    .tap { |sorted| p sorted }
    .whatever
if you're so inclined.

In the second you're not using the return value, so

  db.transaction do
    db[:count] += 1
    db[:count]
  end

would seem appropriate.

I'm using this in DSL-type code:

  person = Person :name => "Arthur" do
    age 42
  end

and find it more aesthetically pleasing than:

  person = Person(:name => "Arthur") {
    age 42
  }

Just my tuppenceworth :)

Regards,
Sean