Action + return + if on single line?

Is it possible to have an if-statement that both do something and
returns from a method - in one single line? I often have code where an
action has to be performed conditionally and then exit/return/break, and
an if/end-block seems like such a waste.

Best regards,

Jari W.

On Dec 18, 2007 3:43 PM, Jari W.
[email protected] wrote:

Is it possible to have an if-statement that both do something and
returns from a method - in one single line? I often have code where an
action has to be performed conditionally and then exit/return/break, and
an if/end-block seems like such a waste.

return “string”.upcase if true

Blessings,
TwP

Is it possible to have an if-statement that both do something and
returns from a method - in one single line? I often have code where
an action has to be performed conditionally and then exit/return/
break, and an if/end-block seems like such a waste.

There’s good old
something and return if condition

If something doesn’t return a value that evaluates to true, you can
always do something like
[something] and return if condition

-Ben

Tim P. wrote:

On Dec 18, 2007 3:43 PM, Jari W.
[email protected] wrote:

Is it possible to have an if-statement that both do something and
returns from a method - in one single line? I often have code where an
action has to be performed conditionally and then exit/return/break, and
an if/end-block seems like such a waste.

return “string”.upcase if true

Ah, of course. And that works equally good with break and next as well,
didn’t know that! Thanks!!

Best regards,

Jari W.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs