Project

General

Profile

Actions

Bug #5571

closed

Syntax Error when put String eql? with logical operation ("||") or "("&&")

Added by rongyj (Yongjun Rong) over 12 years ago. Updated over 12 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.0.0dev (2011-11-04 trunk 33634) [x86_64-darwin10.8.0]
Backport:
[ruby-core:40742]

Description

There has a syntax error when try to connect two string eqls? with the logical operation ("||") and ("&&") as below
{code}
ruby-head :001 > test="test"
=> "test"
ruby-head :002 > puts test.eql?("test") || test.eql?("test1")
true
=> nil
ruby-head :003 > puts test.eql?"test" || test.eql?"test1"
SyntaxError: (irb):3: syntax error, unexpected tSTRING_BEG, expecting $end
puts test.eql?"test" || test.eql?"test1"
^
from /usr/local/rvm/rubies/ruby-head/bin/irb:16:in <main>' ruby-head :004 > puts (test.eql?("test") || test.eql?("test1")) true => nil ruby-head :005 > puts (test.eql?"test" || test.eql?"test1") SyntaxError: (irb):5: syntax error, unexpected tSTRING_BEG, expecting ')' puts (test.eql?"test" || test.eql?"test1") ^ (irb):5: syntax error, unexpected ')', expecting $end from /usr/local/rvm/rubies/ruby-head/bin/irb:16:in '
ruby-head :006 >
{code}

Updated by rongyj (Yongjun Rong) over 12 years ago

ruby-head :001 > test="test"
=> "test"
ruby-head :002 > if test.eql?"test" || test.eql?"test1" ; puts "true"; end
SyntaxError: (irb):2: syntax error, unexpected tSTRING_BEG, expecting keyword_then or ';' or '\n'
if test.eql?"test" || test.eql?"test1" ; puts "true"; end
^
(irb):2: syntax error, unexpected keyword_end, expecting $end
from /usr/local/rvm/rubies/ruby-head/bin/irb:16:in `'

Updated by hasari (Hiro Asari) over 12 years ago

The || operator is binding the second test.eql? to "test", effectively turning the statement into

test.eql? ("test" || test.eql?) "test1"

Updated by nobu (Nobuyoshi Nakada) over 12 years ago

  • Category set to core
  • Status changed from Open to Rejected

Yes, because command call (method call without parentheses) has lower precedence than || operator.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0