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 > Variable / Method Ambiguity
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Variable / Method Ambiguity

Réponse
 
LinkBack Outils de la discussion
Vieux 17/09/2007, 02h18   #1 (permalink)
Summercool Summercool
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Variable / Method Ambiguity

the following program for showing Variable / Method Ambiguity

def a
print "Function 'a' called\n"
99
end

for i in 1..2
if i == 2
print "a=", a, "\n"
else
a = 1
print "a=", a, "\n"
end
end

will produce the result:

a=1
Function 'a' called
a=99

(as described in the PickAx2 book, p. 329)

So at first I thought a Ruby program is interpreted? If interpreted,
then the interpreter will see the "a = 1" at first as treat "a" as a
variable, and then the second time it sees "a", the interpreter should
treat it as a variable again, not as a method. So does that mean a
Ruby program is not interpreted but compiled into some bytecode first?
But then, sometimes I run a Ruby program and then it can run all the way
until it see an "undefined local variable"... so that means it probably
is not compiled... or else it would have stopped without running
anything at all.

So is a Ruby program interpreted or compiled?
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 17/09/2007, 08h00   #2 (permalink)
Michael Linfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Variable / Method Ambiguity


> So is a Ruby program interpreted or compiled?


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

  Réponse avec citation
Vieux 17/09/2007, 08h49   #3 (permalink)
David A. Black
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Variable / Method Ambiguity

Hi --

On Mon, 17 Sep 2007, Summercool Summercool wrote:

> the following program for showing Variable / Method Ambiguity
>
> def a
> print "Function 'a' called\n"
> 99
> end
>
> for i in 1..2
> if i == 2
> print "a=", a, "\n"
> else
> a = 1
> print "a=", a, "\n"
> end
> end
>
> will produce the result:
>
> a=1
> Function 'a' called
> a=99
>
> (as described in the PickAx2 book, p. 329)
>
> So at first I thought a Ruby program is interpreted? If interpreted,
> then the interpreter will see the "a = 1" at first as treat "a" as a
> variable, and then the second time it sees "a", the interpreter should
> treat it as a variable again, not as a method.


No, because there's no such variable in scope. The second time through
the loop, there's no assignment to a, and the first variable a has
gone out of scope. So the only thing that "a" could mean, outside of
an assignment, is the method a.


David

> So does that mean a
> Ruby program is not interpreted but compiled into some bytecode first?
> But then, sometimes I run a Ruby program and then it can run all the way
> until it see an "undefined local variable"... so that means it probably
> is not compiled... or else it would have stopped without running
> anything at all.
>
> So is a Ruby program interpreted or compiled?
> --
> Posted via http://www.ruby-forum.com/.
>


--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

  Réponse avec citation
Vieux 17/09/2007, 10h53   #4 (permalink)
Summercool Summercool
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Variable / Method Ambiguity

On Sep 17, 12:49 am, "David A. Black" <dbl...@rubypal.com> wrote:
>
> No, because there's no such variable in scope. The second time through
> the loop, there's no assignment to a, and the first variable a has
> gone out of scope. So the only thing that "a" could mean, outside of
> an assignment, is the method a.


wow... i thought the "a" variable is kind of like in C or Python:


for (i = 1; i <= 2; i++) {
int a;

if (i==2) {
...
} else {
...
}

}

so that the "a" exists all inside the for loop... maybe that's not
right.

now, we can view the variable "a" as existing in the else block but no
where else in the original Ruby code?

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

  Réponse avec citation
Vieux 17/09/2007, 15h29   #5 (permalink)
Daniel DeLorme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Variable / Method Ambiguity

Summercool Summercool wrote:
> So at first I thought a Ruby program is interpreted? If interpreted,
> then the interpreter will see the "a = 1" at first as treat "a" as a
> variable, and then the second time it sees "a", the interpreter should
> treat it as a variable again, not as a method. So does that mean a
> Ruby program is not interpreted but compiled into some bytecode first?
> But then, sometimes I run a Ruby program and then it can run all the way
> until it see an "undefined local variable"... so that means it probably
> is not compiled... or else it would have stopped without running
> anything at all.
>
> So is a Ruby program interpreted or compiled?


It is interpreted but before being interpreted it is parsed. And whether
a symbol is a variable or not is determined at parse time: when the
parser sees the assignment "a = 1", it assumes that 'a' is a variable
for all subsequent *lines* of the source code, until the end of the
method or block where the assignment occured. It is has nothing to do
with the order in which the code is executed.

Also you'll notice that the "undefined local variable" message is
actually "undefined local variable or method", because ruby can't know
if the unknown symbol was supposed to be a method or a variable.

Daniel

  Réponse avec citation
Vieux 17/09/2007, 17h08   #6 (permalink)
Jeffrey 'jf' Lim
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Variable / Method Ambiguity

On 9/17/07, David A. Black <dblack@rubypal.com> wrote:
> Hi --
>
> On Mon, 17 Sep 2007, Summercool Summercool wrote:
>
> <snip>
> >
> > (as described in the PickAx2 book, p. 329)
> >
> > So at first I thought a Ruby program is interpreted? If interpreted,
> > then the interpreter will see the "a = 1" at first as treat "a" as a
> > variable, and then the second time it sees "a", the interpreter should
> > treat it as a variable again, not as a method.

>
> No, because there's no such variable in scope. The second time through
> the loop, there's no assignment to a, and the first variable a has
> gone out of scope.


so this is lexical scoping, then?


> So the only thing that "a" could mean, outside of
> an assignment, is the method a.
>
>
> David
>


-jf


--
In the meantime, here is your PSA:
"It's so hard to write a graphics driver that open-sourcing it would not ."
-- Andrew Fear, Software Product Manager, NVIDIA Corporation
http://kerneltrap.org/node/7228

  Réponse avec citation
Vieux 18/09/2007, 20h07   #7 (permalink)
Gary Wright
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Variable / Method Ambiguity


On Sep 17, 2007, at 12:08 PM, Jeffrey 'jf' Lim wrote:

> On 9/17/07, David A. Black <dblack@rubypal.com> wrote:
>> Hi --
>>
>> On Mon, 17 Sep 2007, Summercool Summercool wrote:
>>
>> <snip>
>>>
>>> (as described in the PickAx2 book, p. 329)
>>>
>>> So at first I thought a Ruby program is interpreted? If
>>> interpreted,
>>> then the interpreter will see the "a = 1" at first as treat "a" as a
>>> variable, and then the second time it sees "a", the interpreter
>>> should
>>> treat it as a variable again, not as a method.

>>
>> No, because there's no such variable in scope. The second time
>> through
>> the loop, there's no assignment to a, and the first variable a has
>> gone out of scope.

>
> so this is lexical scoping, then?


No, it is just that Ruby has a single-pass parser. The parser needs
to decide if an identifier is a local variable or a zero-argument method
before it has seen the entire text of the file (no look-ahead). Until
the parser sees an assignment of the form "a = ..." it assumes
that 'a' is a method call.

So if/then/else statements do *not* create new lexical scopes.


Gary Wright




  Réponse avec citation
Vieux 18/09/2007, 21h11   #8 (permalink)
7stud --
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Variable / Method Ambiguity

David A. Black wrote:
> No, because there's no such variable in scope. The second time through
> the loop, there's no assignment to a, and the first variable a has
> gone out of scope. So the only thing that "a" could mean, outside of
> an assignment, is the method a.
>


That doesn't seem to bear out. If you switch the if statement around so
that the assignment happens first:

def x
print "Function 'x' called\n"
99
end

for i in 1..2
if i == 1
x = 10
print "x=", x, "\n"
else
print "x=", x, "\n"
end
end


the output is:

x=10
x=10

The second time through that loop, there is no assignment to x and the
previous x has gone out of scope, yet the else clause's use of x does
not result in a method call.

As p. 329 in pickaxe2 carefully explains, it is a ruby parsing rule that
determines what x means. As I read the rule, and as Daniel explains,
and as the two examples show(the 'a' example and the 'x' example), the
rule is: if there is any code that has x=val on a line before the use of
x, then x will be interpreted as a variable. If there is no assignment
to x on a line higher in the code, then x will be interpreted as a
method call.



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

  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 10h42.


Édité par : vBulletin® version 3.7.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18162 seconds with 16 queries