Combine if and do in a single line?

items.each { |item| item.stuff? } if current_user.admin?

Jonathan Rochkind wrote:
[...]

The {| | } and the 'do' syntaxes are interchangeable in all ways.

Almost. I've run across a couple of cases where one works and the other
doesn't -- one creates a Proc and the other doesn't, or something like
that. But that's vanishingly rare, and 99 times out of 100, you will
not go wrong to treat them as interchangeable.

More
of a Ruby question than a Rails one.

Yup.

Best,

Jonathan Rochkind wrote:

[...]

> The {| | } and the 'do' syntaxes are interchangeable in all ways.

Almost. I've run across a couple of cases where one works and the other
doesn't -- one creates a Proc and the other doesn't, or something like
that.

{} binds more tightly than do..end - in some cases this can lead to
your block being passed to the 'wrong' method:

def foo(x)
  puts "foo #{block_given?}"
end

def bar
  puts "bar #{block_given?}"
end

running foo bar {} outputs

bar true
foo false

but running

foo bar do
end

outputs

boo false
foo true

Fred