# Combine if and do in a single line?

**URL:** <https://www.ruby-forum.com/t/combine-if-and-do-in-a-single-line/168983>\
**Category:** Rails\
**Created:** [May 28, 2009, 3:17am UTC](https://www.ruby-forum.com/t/combine-if-and-do-in-a-single-line/168983 "2009-05-28T03:17:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgiorno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mgiorno](https://www.ruby-forum.com/u/mgiorno)\
**Post date:** [May 28, 2009, 3:17am UTC](https://www.ruby-forum.com/t/combine-if-and-do-in-a-single-line/168983/1 "2009-05-28T03:17:54Z")

</div>

Is there a way to combine if and do into a single line?

items.each do |item| if current\_user.admin?  
#do stuff  
end

Thanks!

---

<div class="post-metadata">

**Author:** ![mgiorno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mgiorno](https://www.ruby-forum.com/u/mgiorno)\
**Post date:** [May 28, 2009, 3:50am UTC](https://www.ruby-forum.com/t/combine-if-and-do-in-a-single-line/168983/2 "2009-05-28T03:50:02Z")

</div>

items.each { |item| item.stuff? } if current\_user.admin?

---

<div class="post-metadata">

**Author:** ![mgiorno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mgiorno](https://www.ruby-forum.com/u/mgiorno)\
**Post date:** [May 30, 2009, 7:31pm UTC](https://www.ruby-forum.com/t/combine-if-and-do-in-a-single-line/168983/3 "2009-05-30T19:31:04Z")

</div>

Mario Gr wrote:

> Is there a way to combine if and do into a single line?
> 
> items.each do |item| if current\_user.admin?  
> #do stuff  
> end
> 
> Thanks!

Freddy A. wrote:

> items.each { |item| item.stuff? } if current\_user.admin?

Which you actually COULD write like this too, but I’m not sure if it  
would be considered easily readable:

items.each do |item|  
#do stuff  
end if current\_user.admin?

The {| | } and the ‘do’ syntaxes are interchangeable in all ways. More  
of a Ruby question than a Rails one.

---

<div class="post-metadata">

**Author:** ![mgiorno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mgiorno](https://www.ruby-forum.com/u/mgiorno)\
**Post date:** [May 30, 2009, 8:03pm UTC](https://www.ruby-forum.com/t/combine-if-and-do-in-a-single-line/168983/4 "2009-05-30T20:03:02Z")

</div>

Jonathan R. 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,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mgiorno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mgiorno](https://www.ruby-forum.com/u/mgiorno)\
**Post date:** [May 30, 2009, 8:26pm UTC](https://www.ruby-forum.com/t/combine-if-and-do-in-a-single-line/168983/5 "2009-05-30T20:26:26Z")

</div>

Frederick C. wrote:

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

Interesting, makes sense.

Personally, that’s one reason I don’t like making method calls without  
parens, or doing anything else that relies on non-obvious  
order-of-evaluation-binding just to save a couple of parens.

foo( bar {} )  
or  
foo( bar do  
end )

will both do the same thing. Although the latter is kind of weird style,  
if I need a multi-line block for bar, I’d personally just use a  
temporary var instead.

result = bar do  
end  
foo(result)

But everyone’s got their own style, I guess. Of course you COULD use  
{||} with multi-lines too, but it would also be stylistically weird in  
my opinion in that case.

foo bar {|a|  
stuff  
more stuff  
}

That’s just weird. And if I DID it, I’d still want to put parens in  
around foo’s argument, which would make it even weirder looking.

Jonathan

---

<div class="post-metadata">

**Author:** ![mgiorno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mgiorno](https://www.ruby-forum.com/u/mgiorno)\
**Post date:** [May 30, 2009, 8:16pm UTC](https://www.ruby-forum.com/t/combine-if-and-do-in-a-single-line/168983/6 "2009-05-30T20:16:34Z")

</div>

On May 30, 7:03 pm, Marnen Laibow-Koser \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> Jonathan R. 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
