Action + return + if on single line?

J

Jari Williamsson

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 Williamsson
 
T

Tim Pease

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
 
B

Benjamin Stiglitz

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

There=92s good old
something and return if condition

If something doesn=92t return a value that evaluates to true, you can =20=

always do something like
[something] and return if condition

-Ben=
 
J

Jari Williamsson

Tim said:
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 Williamsson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,366
Members
48,055
Latest member
RacheleCar

Latest Threads

Top