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
Oops I forgot to ask
which one is faster
if x == 5
return true
else
return false
end
or the conditional operator ? :
x == 5 ? true : false
Shuaib Z. wrote:
but if i do not use return it works
x == 5 ? true : false
any idea
That’s the format of the ?: operator.
you could do:
z = (x==5)?true: false
return z
Cheers,
Mohit.
11/6/2007 | 6:11 PM.
Shuaib Z. wrote:
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.
Cheers,
Peter
http://www.rubyrailways.com
http://scrubyt.org
On 11/6/07, Shuaib Z. [email protected] 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
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
Shuaib Z. 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
puts f
–>false
Hi all,
On Tue, 2007-11-06 at 19:33 +0900, 7stud – wrote:
end
puts f
–>false
How about:
return (x == 5)
Cheers,
Arlen
Peter S. wrote:
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:
x = (x == 5) ? (return true) : (return false)
cheers
MoisesMachado
On Nov 6, 3:11 am, Shuaib Z. [email protected] wrote:
Oops I forgot to ask
which one is faster
if x == 5
return true
else
return false
end
or the conditional operator ? :
x == 5 ? true : false
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
values = (1…8).to_a * 500_000
method_names = %w| ternary_return1 ternary_return2
if_return1 if_return2
ternary_expression if_expression |
Benchmark.bmbm{ |x|
method_names.each{ |name|
meth = method( name )
x.report( name ){
values.each{ |n|
meth.call( n )
}
}
}
}
Rehearsal ------------------------------------------------------
ternary_return1 6.125000 0.000000 6.125000 ( 6.157000)
ternary_return2 5.859000 0.000000 5.859000 ( 5.906000)
if_return1 6.016000 0.000000 6.016000 ( 6.047000)
if_return2 5.906000 0.000000 5.906000 ( 5.953000)
ternary_expression 5.453000 0.000000 5.453000 ( 5.484000)
if_expression 5.672000 0.000000 5.672000 ( 5.687000)
-------------------------------------------- total: 35.031000sec
user system total real
ternary_return1 6.047000 0.000000 6.047000 ( 6.078000)
ternary_return2 6.250000 0.000000 6.250000 ( 6.282000)
if_return1 6.032000 0.000000 6.032000 ( 6.063000)
if_return2 6.157000 0.000000 6.157000 ( 6.187000)
ternary_expression 5.656000 0.000000 5.656000 ( 5.703000)
if_expression 5.750000 0.000000 5.750000 ( 5.781000)
Shuaib Z. wrote:
Oops I forgot to ask
which one is faster
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 ?:
cond ? (command1; command2; command3) : command4
and one line with if:
if cond then command1 else command2 end
cheers
MoisesMachado
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
def boolean_expression( x )
x == 5
end
def return_boolean( x )
return x == 5
end
values = (1…8).to_a * 1_000_000
method_names = %w| ternary_return1 ternary_return2
if_return1 if_return2
ternary_expression if_expression
boolean_expression return_boolean |
Benchmark.bmbm{ |x|
method_names.each{ |name|
meth = method( name )
x.report( name ){
values.each{ |n|
meth.call( n )
}
}
}
}
Rehearsal ------------------------------------------------------
ternary_return1 12.125000 0.000000 12.125000 ( 12.172000)
ternary_return2 11.750000 0.000000 11.750000 ( 11.812000)
if_return1 11.797000 0.000000 11.797000 ( 11.813000)
if_return2 11.453000 0.000000 11.453000 ( 11.484000)
ternary_expression 10.516000 0.000000 10.516000 ( 10.578000)
if_expression 11.218000 0.000000 11.218000 ( 11.250000)
boolean_expression 9.719000 0.000000 9.719000 ( 9.782000)
return_boolean 10.797000 0.000000 10.797000 ( 10.812000)
-------------------------------------------- total: 89.375000sec
user system total real
ternary_return1 11.578000 0.000000 11.578000 ( 11.625000)
ternary_return2 12.141000 0.000000 12.141000 ( 12.188000)
if_return1 11.765000 0.000000 11.765000 ( 11.812000)
if_return2 12.031000 0.000000 12.031000 ( 12.078000)
ternary_expression 10.969000 0.000000 10.969000 ( 10.999000)
if_expression 11.078000 0.000000 11.078000 ( 11.125000)
boolean_expression 10.093000 0.000000 10.093000 ( 10.125000)
return_boolean 11.141000 0.000000 11.141000 ( 11.188000)