[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Action + return + if on single line?

Jari Williamsson

12/18/2007 10:44:00 PM

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

3 Answers

Tim Pease

12/18/2007 10:54:00 PM

0

On Dec 18, 2007 3:43 PM, Jari Williamsson
<jari.williamsson@mailbox.swipnet.se> 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

Benjamin Stiglitz

12/18/2007 10:56:00 PM

0

> 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=

Jari Williamsson

12/18/2007 11:08:00 PM

0

Tim Pease wrote:
> On Dec 18, 2007 3:43 PM, Jari Williamsson
> <jari.williamsson@mailbox.swipnet.se> 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 Williamsson