[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

evaluation order

Zehao

2/13/2005 5:56:00 AM

One interesting thing is when running the following codes

File.open("testfile") |aFile| do
print a while a=aFile.gets
end

one will get an error indicating a doesn't exist. This error can be
fixed by adding a line a="" before the print command.

But logically speaking, "print a" should be interpreted after
"a=File.gets". Is this because the interpreter couldn't find the object
"a" when first scanning the second line?

9 Answers

Kaspar Schiess

2/13/2005 8:51:00 AM

0

(In response to news:1108274165.071278.164610@z14g2000cwz.googlegroups.com
by Zehao)

> File.open("testfile") |aFile| do
> print a while a=aFile.gets
> end
>

Isn't that File.open('testfile') do |aFile| ?

k



Robert Klemme

2/13/2005 10:01:00 AM

0


"Zehao" <zehao.chen@gmail.com> schrieb im Newsbeitrag
news:1108274165.071278.164610@z14g2000cwz.googlegroups.com...
> One interesting thing is when running the following codes
>
> File.open("testfile") |aFile| do
> print a while a=aFile.gets
> end
>
> one will get an error indicating a doesn't exist. This error can be
> fixed by adding a line a="" before the print command.
>
> But logically speaking, "print a" should be interpreted after
> "a=File.gets". Is this because the interpreter couldn't find the object
> "a" when first scanning the second line?

I suspect it's the same issue as with local variables: Ruby applies a
certain heuristic to determine whether something is a local var or not.
This heuristic is based on *syntactical* order of occurrence

>> def foo
>> print a
>> a = 10
>> end
=> nil
>> foo
NameError: undefined local variable or method `a' for main:Object
from (irb):2:in `foo'
from (irb):5

See also
http://www.ruby-doc.org/docs/ProgrammingRuby/html/langua...
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressio...

Kind regards

robert

dblack

2/13/2005 1:32:00 PM

0

Martin DeMello

2/13/2005 2:59:00 PM

0

David A. Black <dblack@wobblini.net> wrote:

> (For clarity I've deleted the warning about using '=' in conditional.
> [Matz, can't we get rid of that warning?])

I'd love per-warning suppressibility, but if not, I think this particular
one does more good than harm. (But then, I find the if (a = f(x)) idiom
rather ugly to begin with).

martin

dblack

2/13/2005 3:12:00 PM

0

Robert Klemme

2/13/2005 4:37:00 PM

0


"David A. Black" <dblack@wobblini.net> schrieb im Newsbeitrag
news:Pine.LNX.4.61.0502130707250.22113@wobblini...
> Hi --
>
> On Sun, 13 Feb 2005, Martin DeMello wrote:
>
>> David A. Black <dblack@wobblini.net> wrote:
>>
>>> (For clarity I've deleted the warning about using '=' in conditional.
>>> [Matz, can't we get rid of that warning?])
>>
>> I'd love per-warning suppressibility, but if not, I think this particular
>> one does more good than harm. (But then, I find the if (a = f(x)) idiom
>> rather ugly to begin with).
>
> I'm trying to figure out the pattern behind when it gets triggered or
> not:
>
> irb(main):002:0> while (x = 1); end
> (irb):2: warning: found = in conditional, should be ==
> irb(main):003:0> while (line = gets); end
> abc
> ^D
> => nil
> irb(main):004:0> if (m = /a/.match("b")); end
> => nil
> irb(main):005:0> if (m = /a/); end
> (irb):5: warning: found = in conditional, should be ==
> irb(main):006:0> if (m = "abc".replace("abc")); end
> => nil
> irb(main):007:0> if (m = "abc"); end
> (irb):7: warning: found = in conditional, should be ==
> => nil
>
> When the rhs is a literal, I guess? That at least vastly increases
> the chances of its being a case of a typo. Could it be that Matz is
> several steps ahead of me? :-)

:-))

Note also, that assignment in a conditional really only makes sense for
loops. With "if" and "unless" it's totally superfluous since you can always
do

a=x
if a then...

instead of

if a=x then...

And it's also more readable. So I would not mind if there was *always* a
warning for assignment if it's "if" or "unless". Just my 0.02 EUR though...

Whereas there is no easy equivalent replacement for

while ( line = gets ) ...

Typically, you would need two assignments which is somewhat ugly:

line = gets
while line
....
line = gets
end

Kind regards

robert

Bertram Scharpf

2/13/2005 5:58:00 PM

0

Hi,

Am Montag, 14. Feb 2005, 01:39:57 +0900 schrieb Robert Klemme:
> while ( line = gets ) ...
>
> Typically, you would need two assignments which is somewhat ugly:
>
> line = gets
> while line
> ....
> line = gets
> end

There is

while gets
line = $_.chomp
...
end

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...


Jim Weirich

2/13/2005 7:25:00 PM

0

On Sunday 13 February 2005 11:39 am, Robert Klemme wrote:
> Note also, that assignment in a conditional really only makes sense for
> loops. With "if" and "unless" it's totally superfluous since you can
> always do
>
> a=x
> if a then...

I will occasionally use the a=x in an elsif statement. Generally when the 'x'
part looks up something that I want to process in the elsif part. E.g.

if parent.nil?
handle_no_parent
elsif children = parent.get_children
do_somthing_with(children)
else
do_something_else
end

Probably a bad example, but you get my drift.

--
-- Jim Weirich jim@weirichhouse.org http://onest...
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)


Robert Klemme

2/15/2005 10:08:00 PM

0


"Bertram Scharpf" <lists@bertram-scharpf.de> schrieb im Newsbeitrag
news:20050213171916.GA31424@homer.bertram-scharpf...
> Hi,
>
> Am Montag, 14. Feb 2005, 01:39:57 +0900 schrieb Robert Klemme:
>> while ( line = gets ) ...
>>
>> Typically, you would need two assignments which is somewhat ugly:
>>
>> line = gets
>> while line
>> ....
>> line = gets
>> end
>
> There is
>
> while gets
> line = $_.chomp
> ...
> end
>
> Bertram

That's a special case that works only because gets() does not only return
the value but assigns it to $_ as a side effect. In the general case you
need two assignments unless you put it into the while condition.

Kind regards

robert