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 > Extrange behaviour using "if XXX ..." and "XXX if ..."
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Extrange behaviour using "if XXX ..." and "XXX if ..."

Réponse
 
LinkBack Outils de la discussion
Vieux 30/03/2008, 01h03   #1
Iñaki Baz Castillo
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Extrange behaviour using "if XXX ..." and "XXX if ..."

Hi, note the difference in those both cases:

a)
if a=3D"QWEQWE" : a end
=3D> "QWEQWE"

b)
a if a=3D"QWEQWE"
NameError: undefined local variable or method `a' for main:Object


I know that the behaviour in a) is no recommended and maybe deprecated in a=
=20
future, but why it works in a) and not in b) ?


Thanks.



=2D-=20
I=C3=B1aki Baz Castillo

  Réponse avec citation
Vieux 30/03/2008, 01h22   #2
Tim Hunter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Extrange behaviour using "if XXX ..." and "XXX if ..."

Iñaki Baz Castillo wrote:
> Hi, note the difference in those both cases:
>
> a)
> if a="QWEQWE" : a end
> => "QWEQWE"
>
> b)
> a if a="QWEQWE"
> NameError: undefined local variable or method `a' for main:Object
>
>
> I know that the behaviour in a) is no recommended and maybe deprecated in a
> future, but why it works in a) and not in b) ?


A method call without parentheses and a plain variable look the same, so
Ruby tries to determine between the two by looking for an assignment as
it parses your script. If it sees you assigning a value to "a", then it
knows that "a" is a variable, otherwise it assumes that "a" is a method
call. Parsing occurs from the top down and from left to right.

In case (a) the assignment to "a" occurs before any other reference, so
Ruby knows it's a variable.

In case (b), there has not yet been an assignment before the reference
to a, so Ruby assumes that "a" is a method call, yet there is no
definition for a so you get the message.


--
RMagick: http://rmagick.rubyforge.org/
RMagick 2: http://rmagick.rubyforge.org/rmagick2.html

  Réponse avec citation
Vieux 30/03/2008, 01h41   #3
Iñaki Baz Castillo
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Extrange behaviour using "if XXX ..." and "XXX if ..."

El Domingo, 30 de Marzo de 2008, Tim Hunter escribi=C3=B3:

> In case (b), there has not yet been an assignment before the reference
> to a, so Ruby assumes that "a" is a method call, yet there is no
> definition for a so you get the message.


b)
a if a=3D"QWEQWE"
NameError: undefined local variable or method `a' for main:Object


But why the first "a" is interpreted before the "if" stament? Imagine this=
=20
example:

defined?a
=3D> nil
a=3D"BLABLA" if 2 =3D=3D 3
=3D> nil
a
=3D> nil

After this code "a" will remain "nil" (or the previous value it had), so:
a=3D"BLABLA"
hasn't been interpreted since the "if" stament failed.

Also note this other example:
=20
defined?kk
=3D> nil
kk if nil
=3D> nil

Why in this last case the code doesn't return:
"undefined local variable or method kk' "
?

I assume the reason: "kk" is not interpreted if the condition fails.
In b) the first "a" is then interpreted AFTER the condition success, so in=
=20
that moment "a" has been already declared (into the condition), so it would=
=20
exist and not give an error.

Am I not right?



=2D-=20
I=C3=B1aki Baz Castillo

  Réponse avec citation
Vieux 30/03/2008, 02h06   #4
Tim Hunter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Extrange behaviour using "if XXX ..." and "XXX if ..."

Iñaki Baz Castillo wrote:
> El Domingo, 30 de Marzo de 2008, Tim Hunter escribió:
>
>> In case (b), there has not yet been an assignment before the reference
>> to a, so Ruby assumes that "a" is a method call, yet there is no
>> definition for a so you get the message.

>
> b)
> a if a="QWEQWE"
> NameError: undefined local variable or method `a' for main:Object
>
>
> But why the first "a" is interpreted before the "if" stament? Imagine this
> example:
>
> defined?a
> => nil
> a="BLABLA" if 2 == 3
> => nil
> a
> => nil
>
> After this code "a" will remain "nil" (or the previous value it had), so:
> a="BLABLA"
> hasn't been interpreted since the "if" stament failed.
>
> Also note this other example:
>
> defined?kk
> => nil
> kk if nil
> => nil
>
> Why in this last case the code doesn't return:
> "undefined local variable or method kk' "
> ?
>
> I assume the reason: "kk" is not interpreted if the condition fails.
> In b) the first "a" is then interpreted AFTER the condition success, so in
> that moment "a" has been already declared (into the condition), so it would
> exist and not give an error.
>
> Am I not right?
>
>
>


You must distinguish between parsing and execution. Ruby parses your
program before Ruby executes your program.

Ruby decides how to interpret the reference to a during parsing, before
Ruby executes the if statements in your examples. Their success or
failure has nothing to do with the decision. In this case

a if a = "qweqwe"

Remember parsing occurs from left to right. Ruby sees the reference to a
to the left of the assignment to a. When Ruby first encounters a
reference to a it assumes that since it has not yet seen any assigment
to a, then a must be a method name.


--
RMagick: http://rmagick.rubyforge.org/
RMagick 2: http://rmagick.rubyforge.org/rmagick2.html

  Réponse avec citation
Vieux 30/03/2008, 05h17   #5
Iñaki Baz Castillo
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Extrange behaviour using "if XXX ..." and "XXX if ..."

El Domingo, 30 de Marzo de 2008, Tim Hunter escribi=C3=B3:

> You must distinguish between parsing and execution. Ruby parses your
> program before Ruby executes your program.
>
> Ruby decides how to interpret the reference to a during parsing, before
> Ruby executes the if statements in your examples. Their success or
> failure has nothing to do with the decision. In this case
>
> a if a =3D "qweqwe"
>
> Remember parsing occurs from left to right. Ruby sees the reference to a
> to the left of the assignment to a. When Ruby first encounters a
> reference to a it assumes that since it has not yet seen any assigment
> to a, then a must be a method name.


Ok, I understand. Thanks a lot.

=2D-=20
I=C3=B1aki Baz Castillo

  Réponse avec citation
Vieux 30/03/2008, 06h47   #6
matt neuburg
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Extrange behaviour using "if XXX ..." and "XXX if ..."

Iñaki Baz Castillo <ibc@aliax.net> wrote:

> El Domingo, 30 de Marzo de 2008, Tim Hunter escribió:
>
> > You must distinguish between parsing and execution. Ruby parses your
> > program before Ruby executes your program.
> >
> > Ruby decides how to interpret the reference to a during parsing, before
> > Ruby executes the if statements in your examples. Their success or
> > failure has nothing to do with the decision. In this case
> >
> > a if a = "qweqwe"
> >
> > Remember parsing occurs from left to right. Ruby sees the reference to a
> > to the left of the assignment to a. When Ruby first encounters a
> > reference to a it assumes that since it has not yet seen any assigment
> > to a, then a must be a method name.

>
> Ok, I understand. Thanks a lot.


I asked the same question a few months ago, and got some great answers:

<http://groups.google.com/group/comp....ad/thread/f03e
291e32ea2570/49ca1f19297bece6?lnk=st&q=#49ca1f19297bece6>

Understanding this point has been very useful to me subsequently...! m.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/
Leopard - http://www.takecontrolbooks.com/leop...stomizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com
  Réponse avec citation
Vieux 30/03/2008, 23h27   #7
Iñaki Baz Castillo
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Extrange behaviour using "if XXX ..." and "XXX if ..."

El Domingo, 30 de Marzo de 2008, matt neuburg escribi=F3:
> I asked the same question a few months ago, and got some great answers:
>
> <http://groups.google.com/group/comp....ad/thread/f03e
> 291e32ea2570/49ca1f19297bece6?lnk=3Dst&q=3D#49ca1f19297bece6>
>
> Understanding this point has been very useful to me subsequently...! m.


Thanks for the link matt,it's very useful to understand this "issue".

Regards.

=2D-=20
I=F1aki Baz Castillo

  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 13h48.


Édité par : vBulletin®
Copyright ©2000 - 2009, 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 0,57374 seconds with 15 queries