&& Syntax

L

Lyndon Samson

Yes, I'm a rube-noob

-- ok
if 1 == 1 &&
2 == 2
puts "TRUE"
end

-- syntax error
if 1 == 1
&& 2 == 2
puts "TRUE"
end

The second form is more convienient as you can easily comment out the
line, but it causes a syntax error.

I guess the interpreter need to know where the conditional finishes
and the statements begin?
 
R

Ralf Müller

Yes, I'm a rube-noob

-- ok
if 1 == 1 &&
2 == 2
puts "TRUE"
end

-- syntax error
if 1 == 1
&& 2 == 2
puts "TRUE"
end

The second form is more convienient as you can easily comment out the
line, but it causes a syntax error.

I guess the interpreter need to know where the conditional finishes
and the statements begin?

What about:

irb(main):018:0> if 1 == 1 \
irb(main):019:2* && 2 == 2
irb(main):020:1> p 'df'; end
"df"
=> nil
 
P

Patrick Hurley

Yes, I'm a rube-noob

-- ok
if 1 == 1 &&
2 == 2
puts "TRUE"
end

-- syntax error
if 1 == 1
&& 2 == 2
puts "TRUE"
end

The second form is more convienient as you can easily comment out the
line, but it causes a syntax error.

I guess the interpreter need to know where the conditional finishes
and the statements begin?

Ruby has a very relaxed parser -- that often (unlike C/Java/etc) uses
EOL to delimit expressions. If the parser scans an obvious hanging
statement (like the &&) it will determine that the expression spans to
the next line. You can use the \ to force the span. If you are after a
format that allows for easy commenting out of lines try:

if expr &&
expr &&
expr &&
true
p true
end

Patrick
 
N

nobu.nokada

Hi,

At Fri, 1 Apr 2005 20:29:52 +0900,
Lyndon Samson wrote in [ruby-talk:136283]:
I guess the interpreter need to know where the conditional finishes
and the statements begin?

The interpreter knows the conditional finishes at the end of
line. Note that newlines are significant in Ruby.
 

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,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top