Ternary statement needing parenthesis..(?)

Hello, just wondering why is it that I have to use the parenthesis in
the below ternary statement? (A.) works fine but (B.) bombs out with a
syntax error:-

(1) 60 > 55 ? puts("when true") : puts("when false")
(2) 60 > 55 ? puts "when true" : puts("when false")

cheers Michelle

···

--
Posted via http://www.ruby-forum.com/.

It doesn't know that the colon is delimiting the ternary, it thinks it's
doing something like denoting a symbol, or a hash, or beginning a scope
resolution (Larry's First Law of Language Redesign: Everyone wants the
colon.)

···

On Mon, Aug 15, 2011 at 8:05 AM, Michelle Pace <michelle@michellepace.com>wrote:

Hello, just wondering why is it that I have to use the parenthesis in
the below ternary statement? (A.) works fine but (B.) bombs out with a
syntax error:-

(1) 60 > 55 ? puts("when true") : puts("when false")
(2) 60 > 55 ? puts "when true" : puts("when false")

cheers Michelle

--
Posted via http://www.ruby-forum.com/\.

puts thinks you're trying to start referencing symbol (ala :symbol) as part of additional arguments, as ?: is a low precedence operator. Another example of a non puts call that will fail:

60 > 55 ? exec "ls" : puts "when false"

In cases of outputting strings, I tend to put the puts outside of the conditional and simply return the value:

puts (65 > 55) ? "when true" : "when false"

Regards,
Chris White
http://www.twitter.com/cwgem

···

On Aug 15, 2011, at 6:05 AM, Michelle Pace wrote:

Hello, just wondering why is it that I have to use the parenthesis in
the below ternary statement? (A.) works fine but (B.) bombs out with a
syntax error:-

(1) 60 > 55 ? puts("when true") : puts("when false")
(2) 60 > 55 ? puts "when true" : puts("when false")

Great, thanks Josh. I get it now. And thanks Chris for the suggestion of
simply returning a value. Thats works for me! cheers, Michelle

···

--
Posted via http://www.ruby-forum.com/.