in my program I am using a lot of true false conditions. So, I want to
shorten my code by using the conditional operator ? : but when I use the
word return it gives an error which i do not really know why?
e.g. x == 5 ? return true : return false
SyntaxError: compile error
(irb):19: syntax error, unexpected kTRUE
in my program I am using a lot of true false conditions. So, I want to
shorten my code by using the conditional operator ? : but when I use the
word return it gives an error which i do not really know why?
e.g. x == 5 ? return true : return false
SyntaxError: compile error
(irb):19: syntax error, unexpected kTRUE
in my program I am using a lot of true false conditions. So, I want to
shorten my code by using the conditional operator ? : but when I use the
word return it gives an error which i do not really know why?
e.g. x == 5 ? return true : return false
SyntaxError: compile error
(irb):19: syntax error, unexpected kTRUE
but if i do not use return it works
x == 5 ? true : false
The ternary operator ? : evaluates to a value, so it expects values (or expressions evaluating to values, but not statements as in your example) after ? and : . You would also not write
x = return 5
because during an assignment, Ruby expects a value on the right side of the equation mark.
in my program I am using a lot of true false conditions. So, I want to
shorten my code by using the conditional operator ? : but when I use the
word return it gives an error which i do not really know why?
e.g. x == 5 ? return true : return false
SyntaxError: compile error
(irb):19: syntax error, unexpected kTRUE
but if i do not use return it works
x == 5 ? true : false
any idea
def f
x = 4
x==5 ? (return true) : (return false)
end
you may write it as return x == 5 ? true : false
and in this case you can omit the "? true : false", because the value
of x == 5 is just that.
so in your case write just
return x == 5
and if it is the lase statement, you can event omit the return
···
On 11/6/07, Shuaib Zahda <shuaib.zahda@gmail.com> wrote:
Hi all
in my program I am using a lot of true false conditions. So, I want to
shorten my code by using the conditional operator ? : but when I use the
word return it gives an error which i do not really know why?
On Tue, 2007-11-06 at 19:33 +0900, 7stud -- wrote:
Shuaib Zahda wrote:
> Hi all
>
> in my program I am using a lot of true false conditions. So, I want to
> shorten my code by using the conditional operator ? : but when I use the
> word return it gives an error which i do not really know why?
>
> e.g. x == 5 ? return true : return false
> SyntaxError: compile error
> (irb):19: syntax error, unexpected kTRUE
>
> but if i do not use return it works
>
> x == 5 ? true : false
>
> any idea
def f
x = 4
x==5 ? (return true) : (return false)
end
The ternary operator ? : evaluates to a value, so it expects values (or
expressions evaluating to values, but not statements as in your example)
after ? and : . You would also not write
x = return 5
because during an assignment, Ruby expects a value on the right side of
the equation mark.
The ternary expression don't require value/experession but the
assignment does so the problem is precedence rather than value
requirement. A code like this will work becouse parentheses solves the
problem:
def bla(x)
x == 5 ? (return true) : (return false)
end
but this won't becouse of the assignment that requires a
value/expression:
there not really a difference in functionality between the two forms, i
think that it's the same underlying implementation so same speed
(correct me if i'm wrong didn't run the benchs).
for instance I can do a multi command conditional with ?:
They're about the same speed. Where you _will_ see a small performance
gain, however, is leaving off the call to the return method
altogether. The value of the last expression in a method is the return
value. The only time to use return if you need to exit a method early.
(And this practice is considered a bad idea by some people in many
situations.)
require 'benchmark'
def ternary_return1( x )
return ( x==5 ? true : false )
end
def ternary_return2( x )
x==5 ? (return true) : (return false)
end
def if_return1( x )
return (if x == 5
true
else
false
end)
end
def if_return2( x )
if x == 5
return true
else
return false
end
end
def ternary_expression( x )
x == 5 ? true : false
end
def if_expression( x )
if x == 5
true
else
false
end
end
Others have pointed out that (in this particular example) it's foolish
to branch on a boolean value only to return that particular value as a
new literal, so here's a modified test showing the fastest way - just
let the boolean value return itself.
require 'benchmark'
def ternary_return1( x )
return ( x==5 ? true : false )
end
def ternary_return2( x )
x==5 ? (return true) : (return false)
end
def if_return1( x )
return (if x == 5
true
else
false
end)
end
def if_return2( x )
if x == 5
return true
else
return false
end
end
def ternary_expression( x )
x == 5 ? true : false
end
def if_expression( x )
if x == 5
true
else
false
end
end