On Sun, Apr 26, 2009 at 11:48 PM, James Gray <james / grayproductions.net> wr=
ote:
> I hate to be the guy to start another { =85 } vs. do =85 end thread, but =
I have
> some questions I would love to hear opinions on.
>
> I use to just use the rule of { =85 } for one-liners and do =85 end for t=
he
> longer stuff. =A0However, I've recently switched to trying out { =85 } fo=
r the
> times when I care about the return value and do =85 end for the times the
> block is for side effects. =A0For the most part, I do like the new strate=
gy,
> 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. =A0I do care about the retur=
n
> value, but not the return value of the block, so which strategy should I
> use? =A0It seems like do =85 end is more correct, but that seems a lot ug=
lier in
> practice:
>
> =A0arr.sort.tap do |sorted|
> =A0 =A0p sorted
> =A0end.whatever=85
>
> Another example is with a transaction() method for a database. =A0When us=
ing
> such a tool, I often end up with calls where I care about both the side
> effects (transactional behavior) and the return value:
>
> =A0db.transaction {
> =A0 =A0db[:count] +=3D 1
> =A0 =A0db[:count]
> =A0}
>
> 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=85

assuming that the 'p sorted' stands for a multiline statement. In 1.9
you can even do:

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

if you're so inclined.

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

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

would seem appropriate.

I'm using this in DSL-type code:

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

and find it more aesthetically pleasing than:

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

Just my tuppenceworth :)

Regards,
Sean