
-----------------------------------
richcash
Fri Apr 06, 2007 2:25 pm

Can you have multiple statements executed in 'shortened' conditionals?
-----------------------------------
I'm talking about something like this : 
condition ? do_this_if_true : do_this_if_false
But can I have multiple statements executed when the condition is true?
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?

-----------------------------------
PaulButler
Fri Apr 06, 2007 4:04 pm

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:

if conditionA
doA
else if conditionB
doB
else
doC
end


Could be done with nested ternary operators like this:


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
Fri Apr 06, 2007 4:26 pm

Re: Can you have multiple statements executed in 'shortened' conditionals?
-----------------------------------
Also, how would I get an else if in there?You'd have to use an if statement.
if 
    # Block of code to execute.
elsif 
    # 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.
if  then  elsif  then  else  end

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? ;-)

-----------------------------------
PaulButler
Fri Apr 06, 2007 5:35 pm

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:


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
Fri Apr 06, 2007 7:09 pm

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! :) 


I thought of a way to have multiple statements executed. Since statements are expressions in 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
Fri Apr 06, 2007 11:26 pm

RE:Can you have multiple statements executed in \'shortened\' conditionals?
-----------------------------------
Conditionals in Ruby are expressions.

-----------------------------------
wtd
Sat Apr 07, 2007 12:26 am

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! :) 


I thought of a way to have multiple statements executed. Since statements are expressions in 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
Sat Apr 07, 2007 9:01 am

Re: Can you have multiple statements executed in 'shortened' conditionals?
-----------------------------------

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.

-----------------------------------
wtd
Sat Apr 07, 2007 11:09 am

RE:Can you have multiple statements executed in \'shortened\' conditionals?
-----------------------------------
Alternatively you could use begin...end blocks.

-----------------------------------
richcash
Sat Apr 07, 2007 5:53 pm

Re: Can you have multiple statements executed in 'shortened' conditionals?
-----------------------------------


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. :)





Alternatively you could use begin...end blocks.

Ah, yes. That's what I was looking for in the first place.

condition ? begin statement1; statement2; statement3 end : statement4
Cool!

[/edit]
