long expression syntax

R

Rick Hu

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 )
 
M

messju mohr

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.
 
J

Jamis Buck

messju said:
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.
 
M

messju mohr

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?
--
Jamis Buck
(e-mail address removed)

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"'
 
J

Jamis Buck

messju said:
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. ;) "or" is, indeed, an operator. So are '==', '+', 'and',
'&&', 'not', '!', '=~', and so forth.
 
G

Gavin Sinclair

messju mohr wrote:
I don't know where they are authoritatively stated, except in the Ruby
source code. ;) "or" is, indeed, an operator. So are '==', '+', 'and',
'&&', 'not', '!', '=~', and so forth.

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

Gavin
 
J

Jamis Buck

Gavin said:
== 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.
 
T

Tim Hunter

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."
 
D

Dave Brown

:
: > 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. ;) "or" is, indeed, an operator. So are '==', '+', 'and',
: > '&&', 'not', '!', '=~', and so forth.
:
: == and + (among others) are methods, not operators.

a + b
^
Operator

a .+ b
^
Method

Operator evaluation involves method calls, but operators are still
different things from methods. This is at the syntactic level,
not the semantic level.

--Dave
 
R

Rick Hu

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?
 
H

Hal Fulton

Rick said:
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
 
R

Rick Hu

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.
 

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,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top