Long expression syntax

why do I get a syntax error for

puts 1 if ( 1 == 2
    or 1 == 1 )

but there is no syntax error for

puts 1 if ( 1 == 2 or
    1 == 1 )

i don’t get one with the ruby-1.8.1 that’s distributed in debian’s
sid.

···

On Sun, Jan 18, 2004 at 08:10:01AM +0900, Rick Hu wrote:

why do I get a syntax error for

puts 1 if ( 1 == 2
    or 1 == 1 )

but there is no syntax error for

puts 1 if ( 1 == 2 or
    1 == 1 )

A side-effect of Ruby allowing you to use or not use semicolons as you
please.

The Pickaxe says “Ruby expressions and statements are terminated at the
end of a line unless the statement is obviously incomplete – for example
if the last token on a line is an operator or a comma. A semicolon can be
used to separate multiple expressions on a line. You can also put a
backslash at the end of a line to continue it onto the next.”

···

On Sat, 17 Jan 2004 15:07:57 -0800, Rick Hu wrote:

why do I get a syntax error for

puts 1 if ( 1 == 2
    or 1 == 1 )

but there is no syntax error for

puts 1 if ( 1 == 2 or
    1 == 1 )

rick.hu@gol.com (Rick Hu) wrote in message news:e22cc553.0401171507.a8f41a2@posting.google.com

why do I get a syntax error for

puts 1 if ( 1 == 2
    or 1 == 1 )

but there is no syntax error for

puts 1 if ( 1 == 2 or
    1 == 1 )

I just found out that the parensis can start a COMPOSITE statement.
This explains why the first case is a syntax error, and why
( boolean
boolean

boolean )
returns the value of the last value.

messju mohr wrote:

···

On Sun, Jan 18, 2004 at 08:10:01AM +0900, Rick Hu wrote:

why do I get a syntax error for

puts 1 if ( 1 == 2
or 1 == 1 )

but there is no syntax error for

puts 1 if ( 1 == 2 or
1 == 1 )

i don’t get one with the ruby-1.8.1 that’s distributed in debian’s
sid.

I do, with Ruby 1.8.1. But the reason (as far as I’ve been able to
ascertain) is that Ruby’s grammar does not allow newlines before operators.


Jamis Buck
jgb3@email.byu.edu

ruby -h | ruby -e ‘a=;readlines.join.scan(/-(.)[e|Kk(\S*)|le.l(…)e|#!(\S*)/) {|r| a << r.compact.first };puts “\n>#{a.join(%q/ /)}<\n\n”’

messju mohr wrote:

why do I get a syntax error for

puts 1 if ( 1 == 2
or 1 == 1 )

but there is no syntax error for

puts 1 if ( 1 == 2 or
1 == 1 )

i don’t get one with the ruby-1.8.1 that’s distributed in debian’s
sid.

I do, with Ruby 1.8.1. But the reason (as far as I’ve been able to
ascertain) is that Ruby’s grammar does not allow newlines before operators.

i also get one if i run
puts 1 if ( a == b
or a == c )

maybe just because “or” is an operator and “a” isn’t. i avoid newlines
inside ruby-statements because i don’t know the rules i have to
obey. where are they stated?

···

On Sun, Jan 18, 2004 at 08:37:49AM +0900, Jamis Buck wrote:

On Sun, Jan 18, 2004 at 08:10:01AM +0900, Rick Hu wrote:


Jamis Buck
jgb3@email.byu.edu

ruby -h | ruby -e
‘a=;readlines.join.scan(/-(.)[e|Kk(\S*)|le.l(…)e|#!(\S*)/) {|r| a <<
r.compact.first };puts “\n>#{a.join(%q/ /)}<\n\n”’

messju mohr wrote:

i also get one if i run
puts 1 if ( a == b
or a == c )

maybe just because “or” is an operator and “a” isn’t. i avoid newlines
inside ruby-statements because i don’t know the rules i have to
obey. where are they stated?

I don’t know where they are authoritatively stated, except in the Ruby
source code. :wink: “or” is, indeed, an operator. So are ‘==’, ‘+’, ‘and’,
‘&&’, ‘not’, ‘!’, ‘=~’, and so forth.

···


Jamis Buck
jgb3@email.byu.edu

ruby -h | ruby -e ‘a=;readlines.join.scan(/-(.)[e|Kk(\S*)|le.l(…)e|#!(\S*)/) {|r| a << r.compact.first };puts “\n>#{a.join(%q/ /)}<\n\n”’

== and + (among others) are methods, not operators.

Gavin

···

On Sunday, January 18, 2004, 10:59:05 AM, Jamis wrote:

messju mohr wrote:

i also get one if i run
puts 1 if ( a == b
or a == c )

maybe just because “or” is an operator and “a” isn’t. i avoid newlines
inside ruby-statements because i don’t know the rules i have to
obey. where are they stated?

I don’t know where they are authoritatively stated, except in the Ruby
source code. :wink: “or” is, indeed, an operator. So are ‘==’, ‘+’, ‘and’,
‘&&’, ‘not’, ‘!’, ‘=~’, and so forth.

Thanks for all your responses.

Now try these

puts 1 if ( 1 == 2
1 == 1 )

[ syntax is correct. output 1 ]

puts 1 if ( 1 == 1
1 == 2 )

[ syntax is correct. output nothing ]

Somehow,
( boolean
boolean
boolean

boolean )
is a valid syntax and the last boolean value is the value of the expression.

What’s the rationale behind this?

Gavin Sinclair wrote:

== and + (among others) are methods, not operators.

Gavin

You are, of course, correct. However, I still stand by my statement
that == and + are also operators, since they have a precedence and
associativity, which your run-of-the-mill ‘method’ does not have. Also,
it makes it easier to remember where you can and cannot put newlines.

···


Jamis Buck
jgb3@email.byu.edu

ruby -h | ruby -e ‘a=;readlines.join.scan(/-(.)[e|Kk(\S*)|le.l(…)e|#!(\S*)/) {|r| a << r.compact.first };puts “\n>#{a.join(%q/ /)}<\n\n”’

No, they’re operators, which happen to result in a method invocation.
The definition of “operator” is syntactic, not semantic, and
the difference is evident in the way they’re used: a + b vs. a.+(b).

I would expect an unclosed parenthesis to be enough to imply
continuation on the next line, and not generate a syntax error.

-Mark

···

On Sun, Jan 18, 2004 at 09:31:27AM +0900, Gavin Sinclair wrote:

== and + (among others) are methods, not operators.

Rick Hu wrote:

Thanks for all your responses.

Now try these

puts 1 if ( 1 == 2
1 == 1 )

[ syntax is correct. output 1 ]

puts 1 if ( 1 == 1
1 == 2 )

[ syntax is correct. output nothing ]

Somehow,
( boolean
boolean
boolean

boolean )
is a valid syntax and the last boolean value is the value of the expression.

What’s the rationale behind this?

What’s going on here is:

  1. Ruby has two valid statement terminators - semicolon and end-of-line
  2. Expressions can be grouped in parentheses
  3. The line between statements and expressions is blurred in Ruby

Observe that, since Ruby is expression-oriented,
1 == 2
is valid “stand-alone” line of code. It just doesn’t do anything; it’s
evaluated and thrown away.

Ruby has no real concept of a “Boolean,” by the way. false and nil are
treated as false; everything else is treated as true. The value true
exists for convenience, of course.

Hal