Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Can you have multiple statements executed in 'shortened' conditionals?
Index -> Programming, Ruby -> Ruby Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
richcash




PostPosted: Fri Apr 06, 2007 2:25 pm   Post subject: Can you have multiple statements executed in 'shortened' conditionals?

I'm talking about something like this :
Ruby:
condition ? do_this_if_true : do_this_if_false

But can I have multiple statements executed when the condition is true?
Ruby:
condition ? do_this; do_that; and_this : do_this_if_false

The above doesn't work (semicolons).

Also, how would I get an else if in there?
Sponsor
Sponsor
Sponsor
sponsor
PaulButler




PostPosted: Fri Apr 06, 2007 4:04 pm   Post subject: RE:Can you have multiple statements executed in \'shortened\' conditionals?

This is called the conditional operator or ternary operator.

I am not sure about the first question, but for the second question you can nest the operator.

This pseudo-code else-if:
code:

if conditionA
doA
else if conditionB
doB
else
doC
end


Could be done with nested ternary operators like this:

Ruby:

conditionA ? doA : (conditionB ? doB : doC);


Keep in mind that if statements in ruby can return a value, unlike C or Java, so if you are just using the ternary operator so you can return a value, you don't have to.
rdrake




PostPosted: Fri Apr 06, 2007 4:26 pm   Post subject: Re: Can you have multiple statements executed in 'shortened' conditionals?

richcash @ Fri Apr 06, 2007 2:25 pm wrote:
Also, how would I get an else if in there?
You'd have to use an if statement.
Ruby:
if <condition>
    # Block of code to execute.
elsif <condition>
    # Another block of code to execute.
else
    # If all else fails, execute this.
end

If you really wanted things to get ugly, you could do the following as well.
Ruby:
if <condition> then <Block of code to execute.> elsif <condition> then <Another block of code to execute.> else <If all else fails, execute this> end


PaulButler @ Fri Apr 06, 2007 4:04 pm wrote:
Keep in mind that if statements in ruby can return a value, unlike C or Java, so if you are just using the ternary operator so you can return a value, you don't have to.
Statements that return a value, eh? You mean like an expression? Wink
PaulButler




PostPosted: Fri Apr 06, 2007 5:35 pm   Post subject: RE:Can you have multiple statements executed in \'shortened\' conditionals?

They return a value, but they still act like control structures. I think that makes them different from expressions, but I could be wrong.

I realize now that the last line of my post is a bit ambiguous, how's this:

Quote:

Keep in mind that if-statements in ruby can return a value, unlike C or Java, so if you are just using the ternary operator so you can return a value, you don't have to.
richcash




PostPosted: Fri Apr 06, 2007 7:09 pm   Post subject: Re: Can you have multiple statements executed in 'shortened' conditionals?

Thanks for the replies.
Yeah, I've learned if statements and I am aware that they return values, I was using the ternary operator because it looks nicer in place of shorter if statements. I never thought of nested ternary operators, thanks! Smile


I thought of a way to have multiple statements executed. Since statements are expressions in Ruby :
Ruby:
condition ? statement_a && statement_b : statement_c

Is that acceptable, or would it confuse readers? I think it looks better than sticking an elsif in the middle of a line.
wtd




PostPosted: Fri Apr 06, 2007 11:26 pm   Post subject: RE:Can you have multiple statements executed in \'shortened\' conditionals?

Conditionals in Ruby are expressions.
wtd




PostPosted: Sat Apr 07, 2007 12:26 am   Post subject: Re: Can you have multiple statements executed in 'shortened' conditionals?

richcash @ Sat Apr 07, 2007 8:09 am wrote:
Thanks for the replies.
Yeah, I've learned if statements and I am aware that they return values, I was using the ternary operator because it looks nicer in place of shorter if statements. I never thought of nested ternary operators, thanks! Smile


I thought of a way to have multiple statements executed. Since statements are expressions in Ruby :
Ruby:
condition ? statement_a && statement_b : statement_c

Is that acceptable, or would it confuse readers? I think it looks better than sticking an elsif in the middle of a line.


Short-circuiting.

Just use if, elsif and else. Sprinkle in some "then".
PaulButler




PostPosted: Sat Apr 07, 2007 9:01 am   Post subject: Re: Can you have multiple statements executed in 'shortened' conditionals?

wtd @ Sat Apr 07, 2007 12:26 am wrote:
richcash @ Sat Apr 07, 2007 8:09 am wrote:

Ruby:
condition ? statement_a && statement_b : statement_c



Short-circuiting.


Yeah, that's the problem. Unless you know that statement_a will return a true value, you can't be sure that statement_b will be executed.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Apr 07, 2007 11:09 am   Post subject: RE:Can you have multiple statements executed in \'shortened\' conditionals?

Alternatively you could use begin...end blocks.
richcash




PostPosted: Sat Apr 07, 2007 5:53 pm   Post subject: Re: Can you have multiple statements executed in 'shortened' conditionals?

PaulButler wrote:

wtd @ Sat Apr 07, 2007 12:26 am wrote:
richcash @ Sat Apr 07, 2007 8:09 am wrote:

Ruby:
condition ? statement_a && statement_b : statement_c



Short-circuiting.


Yeah, that's the problem. Unless you know that statement_a will return a true value, you can't be sure that statement_b will be executed.


Oh yeah, I forgot about statement_a returning false. You see statement_a in my case will not return a boolean value, it is an assignment of a string or integer. That's why it works for me.

Thanks to both of you for pointing out that it won't work if your statements return boolean values. Smile




[edit]

wtd wrote:

Alternatively you could use begin...end blocks.

Ah, yes. That's what I was looking for in the first place.

code:
condition ? begin statement1; statement2; statement3 end : statement4

Cool!

[/edit]
Display posts from previous:   
   Index -> Programming, Ruby -> Ruby Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: