Expression Evaluation Problem

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

Shannon Fang wrote:

[...]
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 :wink:

daz

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

···

On Oct 27, 2005, at 8:27 PM, Shannon Fang wrote:

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

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.

···

--
Nobu Nakada