Long quotes on multiple lines

R

Robert James

Is there an idiomatic way to continue a long quote on a separate line,
or should I just do:

"If you need help," +
" please dial the operator"
 
W

William James

Robert said:
Is there an idiomatic way to continue a long quote on a separate line,
or should I just do:

"If you need help," +
" please dial the operator"
two \
three"
=> "one two three"
 
N

Nobuyoshi Nakada

Hi,

At Sun, 14 Jan 2007 14:20:54 +0900,
Robert James wrote in [ruby-talk:233900]:
Is there an idiomatic way to continue a long quote on a separate line,
or should I just do:

"If you need help," +
" please dial the operator"

# string literal concatenation
puts "If you need help," \
" please dial the operator"

# escaping new lines
puts "If you need help,\
please dial the operator"

# ditto, with here doc
puts <<EOS
If you need help,\
please dial the operator
EOS
 
J

Joel VanderWerf

Nobuyoshi said:
# string literal concatenation
puts "If you need help," \
" please dial the operator"

Is this deprecated and planned for removal at some point? I seem to
recall so.
 
N

Nobuyoshi Nakada

Hi,

At Sun, 14 Jan 2007 14:55:31 +0900,
Joel VanderWerf wrote in [ruby-talk:233904]:
Is this deprecated and planned for removal at some point? I seem to
recall so.

Maybe in the future, but it's not deprecated nor planned yet
right now, although Matz has mentioned about it somewhere.

And, the last one included a new line at the end.
Instead, another one:

# empty expression interpolation
p "If you need help,#{
} please dial the operator"
 
D

David Krmpotic

Robert said:
Is there an idiomatic way to continue a long quote on a separate line,
or should I just do:

"If you need help," +
" please dial the operator"

Hmm, I don't think you can..

if you do (which you can)

"If you need help
please dial the operator"

you'll get '\n' sneaked in there...

I think that using '+' is the only way. Now the question is if that
affects performance at all.. I wouldn't know.
 
R

Robert Klemme

Hi,

At Sun, 14 Jan 2007 14:20:54 +0900,
Robert James wrote in [ruby-talk:233900]:
Is there an idiomatic way to continue a long quote on a separate line,
or should I just do:

"If you need help," +
" please dial the operator"

# string literal concatenation
puts "If you need help," \
" please dial the operator"

# escaping new lines
puts "If you need help,\
please dial the operator"

# ditto, with here doc
puts <<EOS
If you need help,\
please dial the operator
EOS

Alternative to escaping new lines is replacement:

irb(main):004:0> s="foo
irb(main):005:0" bar
irb(main):006:0" baz".gsub! "\n", ' '
=> "foo bar baz"
irb(main):007:0> s
=> "foo bar baz"

Of course, this is less efficient because it's done at runtime - but
might be ok for constants.

This also works with heredocs.

robert
 
K

Kalman Noel

Robert Klemme:
Alternative to escaping new lines is replacement:

irb(main):004:0> s="foo
irb(main):005:0" bar
irb(main):006:0" baz".gsub! "\n", ' '
=> "foo bar baz"
irb(main):007:0> s
=> "foo bar baz"

Also, if you like something fancy:

require 'facet/string/margin'

x = %Q{
| This
| is
| margin controlled!
}.margin

Regards, Kalman
 
C

Charles Lowe

David said:
Hmm, I don't think you can..

if you do (which you can)

"If you need help
please dial the operator"

you'll get '\n' sneaked in there...

I think that using '+' is the only way. Now the question is if that
affects performance at all.. I wouldn't know.

At a guess, I would say that using + means it has to go through
String#+. If you write something like

class String
alias old_plus :+
def + other
puts "adding strings "#{other}"
old_plus other
end
end

On the other hand, if you use a lexical construct like

"asdf asdf" \
"asdf asdf"

Then the compiler probably does (or at least can do) literal string
concatenation at parse time.
 
D

David Krmpotic

ah ok, so this is the answer Robert was looking for:

"asdf asdf" \
"asdf asdf"

I didn't know how to do this either. Now I know. Cool
 

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
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top