Forum: Ruby Long quotes on multiple lines

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Robert J. (Guest)
on 2007-01-14 07:20
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"
D. K. (Guest)
on 2007-01-14 14:39
Robert J. wrote:
> 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.
Charles L. (Guest)
on 2007-01-15 06:49
David K. wrote:
> Robert J. wrote:
>> 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.

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. K. (Guest)
on 2007-01-15 17:30
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
William J. (Guest)
on 2007-01-19 17:30
(Received via mailing list)
Robert J. wrote:
> 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"
>
> --
> Posted via http://www.ruby-forum.com/.

>> "one \
two \
three"
=> "one two three"
Nobuyoshi N. (Guest)
on 2007-01-19 17:30
(Received via mailing list)
Hi,

At Sun, 14 Jan 2007 14:20:54 +0900,
Robert J. 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
Robert K. (Guest)
on 2007-09-26 00:29
(Received via mailing list)
On 14.01.2007 06:42, Nobuyoshi N. wrote:
> # string literal concatenation
>  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
Joel VanderWerf (Guest)
on 2007-09-26 00:32
(Received via mailing list)
Nobuyoshi N. wrote:
> # 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.
Kalman N. (Guest)
on 2007-09-26 00:35
(Received via mailing list)
Robert K.:
> 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
Nobuyoshi N. (Guest)
on 2007-09-26 00:51
(Received via mailing list)
Hi,

At Sun, 14 Jan 2007 14:55:31 +0900,
Joel VanderWerf wrote in [ruby-talk:233904]:
> > # 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.

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"
This topic is locked and can not be replied to.