J
James Gray
I hate to be the guy to start another { =85 } vs. do =85 end thread, but =
I =20
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 =20=
the longer stuff. However, I've recently switched to trying out { =85 } =
=20
for the times when I care about the return value and do =85 end for the =20=
times the block is for side effects. For the most part, I do like the =20=
new strategy, 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. I do care about the =20
return value, but not the return value of the block, so which strategy =20=
should I use? It seems like do =85 end is more correct, but that seems =20=
a lot uglier in practice:
arr.sort.tap do |sorted|
p sorted
end.whatever=85
Another example is with a transaction() method for a database. When =20
using such a tool, I often end up with calls where I care about both =20
the side effects (transactional behavior) and the return value:
db.transaction {
db[:count] +=3D 1
db[:count]
}
Any thoughts on how edge cases like this mesh with the block strategy?
James Edward Gray II
I =20
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 =20=
the longer stuff. However, I've recently switched to trying out { =85 } =
=20
for the times when I care about the return value and do =85 end for the =20=
times the block is for side effects. For the most part, I do like the =20=
new strategy, 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. I do care about the =20
return value, but not the return value of the block, so which strategy =20=
should I use? It seems like do =85 end is more correct, but that seems =20=
a lot uglier in practice:
arr.sort.tap do |sorted|
p sorted
end.whatever=85
Another example is with a transaction() method for a database. When =20
using such a tool, I often end up with calls where I care about both =20
the side effects (transactional behavior) and the return value:
db.transaction {
db[:count] +=3D 1
db[:count]
}
Any thoughts on how edge cases like this mesh with the block strategy?
James Edward Gray II