[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Long quotes on multiple lines

S. Robert James

1/14/2007 5:21:00 AM

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

9 Answers

William James

1/14/2007 5:38:00 AM

0


Robert James 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-....

>> "one two three"
=> "one two three"

Nobuyoshi Nakada

1/14/2007 5:43:00 AM

0

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

--
Nobu Nakada

Joel VanderWerf

1/14/2007 5:56:00 AM

0

Nobuyoshi Nakada 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.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Nobuyoshi Nakada

1/14/2007 6:45:00 AM

0

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"

--
Nobu Nakada

David Krmpotic

1/14/2007 12:40:00 PM

0

Robert James 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.

--
Posted via http://www.ruby-....

Robert Klemme

1/14/2007 12:53:00 PM

0

On 14.01.2007 06:42, Nobuyoshi Nakada wrote:
> 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

Kalman Noel

1/14/2007 1:51:00 PM

0

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

Charles L.

1/15/2007 4:49:00 AM

0

David Krmpotic wrote:
> Robert James 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.

--
Posted via http://www.ruby-....

David Krmpotic

1/15/2007 3:31:00 PM

0

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

--
Posted via http://www.ruby-....