[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

multi-line strings with """

Navindra Umanee

7/15/2005 10:12:00 AM

Hi,

Why is it that multi-line strings that begin with """ close themselves
when a single " is encountered?

irb(main):006:0> """
irb(main):007:0" This is a multi-line string
irb(main):008:0" with a "
=> "\nThis is a multi-line string\nwith a "

I find this to be somewhat annoying. The Python way seems preferable:

>>> """
... In this string I can have isolated "s without worry
... the only way to end the string is with """
'\nIn this string I can have isolated "s without worry\nthe only way to end thestring is with '

Any chance this behavior will change in the next Ruby? :-)

Thanks,
Navin.




2 Answers

YANAGAWA Kazuhisa

7/15/2005 10:44:00 AM

0

In Message-Id: <20050715061147.A28910@cs.mcgill.ca>
Navindra Umanee <navindra@cs.mcgill.ca> writes:

> Why is it that multi-line strings that begin with """ close themselves
> when a single " is encountered?
>
> irb(main):006:0> """
> irb(main):007:0" This is a multi-line string
> irb(main):008:0" with a "
> => "\nThis is a multi-line string\nwith a "

1. multiple string literals are concatenated.
2. There're no multi-line string specific quotations.

In the above case your string is parsed as "" and "\nThis...." in a
sequence.


> Any chance this behavior will change in the next Ruby? :-)

You can use:

"mere single double-quotation
for multi-line string"

str = <<EOS
or use here document
for multi-line string
EOS


--
kjana@dm4lab.to July 15, 2005
Dreams come true --- excepting yours.



Ryan Leavengood

7/15/2005 3:05:00 PM

0

YANAGAWA Kazuhisa said:
>
> You can use:
>
> "mere single double-quotation
> for multi-line string"
>
> str = <<EOS
> or use here document
> for multi-line string
> EOS

There is also %q (works like single quotes) and %Q (works like double
quotes, i.e. has interpolation):

irb(main):009:0> %q{"Look", you can "use" 'all' "the" "quotes" "you" want
irb(main):010:0' and "on" multiple "lines"
irb(main):011:0' 'yep'}
=> "\"Look\", you can \"use\" 'all' \"the\" \"quotes\" \"you\" want\nand
\"on\" multiple \"lines\"\n'yep'"
irb(main):012:0> %Q{"#{1+1}" and 'blah'
irb(main):013:0" "foo"}
=> "\"2\" and 'blah'\n\"foo\""

Ryan