How to write ruby in multiple lines

A

anakintang

All,

How can I write "s = s1 + s2 + s3" in multiple lines like below:

s= s1
+ s2
+ s3

Thanks
 
J

james.d.masters

You can't.

You can do it differently, though:

s = s1 +
s2 +
s3

If you have an operator at the end of the line, needing operands, Ruby
keeps reading.

-s

You actually can with a trailing backslash:

% irb
irb(main):001:0> s1 = s2 = s3 = 1
=> 1
irb(main):002:0> s = s1 \
irb(main):003:0* + s2 \
irb(main):004:0* + s3
=> 3
 
S

Sylvain Joyeux

All,

How can I write "s = s1 + s2 + s3" in multiple lines like below:

s= s1
+ s2
+ s3

Thanks

Like that
s = s1 +
s2 +
s3

or

s = s1 \
+ s2 \
+ s3

AFAIK, the first form is more in use than the second.
 
G

Glen Pfeiffer

Like that
s = s1 +
s2 +
s3

or

s = s1 \
+ s2 \
+ s3

AFAIK, the first form is more in use than the second.

In the example given, which is obviously contrived, it makes
little difference. But when s1, s2, and s3 are longer
expressions, I find that the second version is more readily
grokked.

Without looking at the end of a line, you can tell that it is a
continuation of a previous line, and while at the end of a line
to can tell that it is continued.

That's my $0.02
 

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

Forum statistics

Threads
474,241
Messages
2,571,223
Members
47,861
Latest member
ACOAlphons

Latest Threads

Top