Expression Evaluation Problem

S

Shannon Fang

Hi,

I have a program which runs ok for a very long time, today I try to modify
it some strange error pops up (before I modify):

c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in `require__':
/dbload.rb:110: void value expression (SyntaxError)
return (@acc.class == Hash) and (@auth.class == Hash) and (@cat.class ==
Hash)
^ from
c:/ruby/lib
/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in `require'
from F:/work/svn/dproc/dproc.rb:5

The problematic source codes are as follow:

dproc.rb:

require 'something'
require 'dbload'

... ...

dbload:
... ...
def loaded?
return (@acc.class == Hash) and (@auth.class == Hash) and (@cat.class ==
Hash)
end
... ...

Now I add a () in the loaded? function make it like:

return ((@acc.class == Hash) and (@auth.class == Hash) and (@cat.class ==
Hash))

The error disappeared!

My question is, which operator has higher priority? return or and?

I have used this code for a long time, but never had this problem before...
Any change in v1.8 probably?

Thank you!
Shannon
 
D

daz

Shannon said:
[...]
My question is, which operator has higher priority? return or and?


/return/ and /and/ are both keywords

return 1 and 2
parses as:
return(1) and 2 # void expression error

The return keyword isn't necessary (as you know) -
- without it there's no conflict.

/&&/ operator has a higher priority than /and/


def loaded?
print "#$c : "
case $c
when 0: # return 1 and 2 # err #-> 0 : nil
when 1: # return(1) and 2 # err #-> 1 : nil
when 2: return(1 and 2) #-> 2 : 2
when 3: return 1 && 2 #-> 3 : 2
when 4: 1 and 2 #-> 4 : 2
when 5: 1 && 2 #-> 5 : 2
end
end

6.times {|$c| p loaded? }

I have used this code for a long time, but never had this problem before...
Any change in v1.8 probably?

I think, if it ever worked, you were lucky ;)

daz
 
E

Eric Hodel

Hi,

I have a program which runs ok for a very long time, today I try to
modify it some strange error pops up (before I modify):

c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in
`require__': ./dbload.rb:110: void value expression (SyntaxError)
return (@acc.class == Hash) and (@auth.class == Hash) and
(@cat.class == Hash)
^ from
c:/ruby/lib
/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in `require'
from F:/work/svn/dproc/dproc.rb:5

The problematic source codes are as follow:

dbload:
def loaded?
return (@acc.class == Hash) and (@auth.class == Hash) and
(@cat.class == Hash)
end

Now I add a () in the loaded? function make it like:

return ((@acc.class == Hash) and (@auth.class == Hash) and
(@cat.class == Hash))

The error disappeared!

My question is, which operator has higher priority? return or and?

return is higher.
I have used this code for a long time, but never had this problem
before... Any change in v1.8 probably?

No.

$ cat x.rb
def which?
return 'return is higher' and 'and is higher'
end
puts which?
$ ruby168 -v x.rb
ruby 1.6.8 (2002-12-24) [powerpc-darwin8.2.0]
x.rb:2: void value expression
$ ruby182-orig -v x.rb
ruby 1.8.2 (2004-12-25) [powerpc-darwin8.0]
x.rb:2: void value expression

Maybe you had:

$ cat y.rb
def which?
return 'return is higher' && '&& is higher'
end
puts which?
$ ruby -v y.rb
ruby 1.8.3 (2005-09-21) [powerpc-darwin8.2.0]
&& is higher
 
N

nobuyoshi nakada

Hi,

At Fri, 28 Oct 2005 12:27:41 +0900,
Shannon Fang wrote in [ruby-talk:163048]:
I have used this code for a long time, but never had this problem before...
Any change in v1.8 probably?

The precedence hasn't been changed and it is checked even in
1.4.0.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top