PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > why return gives error when used with ? :
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
why return gives error when used with ? :

Réponse
 
LinkBack Outils de la discussion
Vieux 06/11/2007, 10h07   #1
Shuaib Zahda
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut why return gives error when used with ? :

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
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 06/11/2007, 10h11   #2
Shuaib Zahda
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why return gives error when used with ? :

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
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 06/11/2007, 10h12   #3
Mohit Sindhwani
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why return gives error when used with ? :

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
>


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.





  Réponse avec citation
Vieux 06/11/2007, 10h13   #4
Marcus Kraßmann
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why return gives error when used with ? :

Shuaib Zahda schrieb:
> 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


Hi Shuaib,

usually you use the "==" operator if you want to assign a value to a
variable. Example:

boolean result = (x == 5 ? true : false);

What you want is:

return (x == 5 ? true : false);
// brackets might be optional

Greetings
Marcus
  Réponse avec citation
Vieux 06/11/2007, 10h19   #5
Peter Szinek
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why return gives error when used with ? :

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


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


  Réponse avec citation
Vieux 06/11/2007, 10h33   #6
7stud --
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why return gives error when used with ? :

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

puts f
-->false
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 06/11/2007, 10h54   #7
Jano Svitok
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why return gives error when used with ? :

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?
>
> 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

  Réponse avec citation
Vieux 06/11/2007, 11h04   #8
Arlen Christian Mart Cuss
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why return gives error when used with ? :

Hi all,

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
>
> puts f
> -->false


How about:

return (x == 5)



Cheers,
Arlen


  Réponse avec citation
Vieux 06/11/2007, 15h21   #9
Moises Machado
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why return gives error when used with ? :

Peter Szinek 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
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 06/11/2007, 15h30   #10
Moises Machado
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why return gives error when used with ? :

Shuaib Zahda 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
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 06/11/2007, 16h11   #11
Phrogz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why return gives error when used with ? :

On Nov 6, 3:11 am, Shuaib Zahda <shuaib.za...@gmail.com> 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)

  Réponse avec citation
Vieux 06/11/2007, 16h28   #12
Phrogz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why return gives error when used with ? :

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)

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 18h21.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 2,92764 seconds with 20 queries