Python -Vs- Ruby: A regexp match to the death!

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Aug 9 03:15:00 CEST 2010


On Sun, 08 Aug 2010 17:43:03 -0700, rantingrick wrote:

> Ha. Ruby does not really have multi line strings. 

Except, of course, it does, as you go on to show.


> Ruby has what they
> call a "Here Doc". Besides picking the most boneheaded name for such an
> object 

It's standard terminology that has been around for a long time in many 
different languages.

http://en.wikipedia.org/wiki/Here_document


> they also introduced and even more boneheaded syntax. To define a
> "Here Doc" (god i hate that name!) you start with double greater than
> ">>" and immediately follow with an identifier token of you choice (it
> can be anything your dirty little mind can come up with.
> 
>>>HEREDOC
> this is the body
> of a
> here doc. Why the
> hell did they not just
> use triple quotes like Python did.
> Now i will need to remember some token to know where' i stopped
> HEREDOC


Incorrect.

[steve at sylar ~]$ irb
irb(main):001:0> s = >>END
SyntaxError: compile error
(irb):1: syntax error
s = >>END
      ^
        from (irb):1
irb(main):002:0> s = <<-END
irb(main):003:0" Multi-line text
irb(main):004:0" goes here
irb(main):005:0" END
=> "Multi-line text\ngoes here\n"
irb(main):006:0> puts s
Multi-line text
goes here
=> nil
irb(main):007:0>


 
> As you can see it is another example of tacked on functionality that was
> not carefully considered before hand.

I disagree. It's an old and venerable technique, and very useful on the 
rare occasion that you have lots of quotation marks in a string.

Whether those rare occasions are common enough to require specialist 
syntax is another question. In Python, the idea is that two heredocs (''' 
and """) is enough for anybody. That makes it difficult to write a string 
literal like, e.g.:

  Python strings have four delimiters:
  (1) single quote '
  (2) double quote "
  (3) single-quote here-doc '''
  (4) double-quote here-doc """

  plus equivalent raw-strings of each kind.

Trying writing that as a single literal in Python without escapes. There 
are work-arounds, of course, like using implicit concatenation, but 
they're ugly.

In Ruby they decided to be more general, so you can define whatever 
heredoc you need to quote whatever literal string you need. That's not 
bone-headed.



-- 
Steven



More information about the Python-list mailing list