From: MRAB on
rantingrick wrote:
> Hello folks,
>
[snip]
> ---------------------
> Strings
> ---------------------
> Single line strings are exactly the same in both languages except in
> Ruby double quoted strings are backslash interpreted and single quote
> strings are basically raw. Except Ruby introduces more cruft (as
> usual) in the form of what i call "lazy man" stings....
>
>>>> a = %w{ one two three}
> ["one", "two", "three"]
>>>> s = %{one two three}
> one two three
>>>> repat = %r{one two three}
> /one two three/
>
> ... only good for hand coding!
>
From Perl.

> ----------------------
> Multi Line Strings
> ----------------------
> Ha. Ruby does not really have multi line strings. Ruby has what they
> call a "Here Doc". Besides picking the most boneheaded name for such
> an object 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
>
> As you can see it is another example of tacked on functionality that
> was not carefully considered before hand. Anyway here are the
> regexp's...
>
> Python: r'""".*?"""'
> Python: r"'''.*?'''"
> Ruby: r'<<(\w+).*?(\1)'
>
Also from Perl.

I don't know what the point of your post was. We already know that we
prefer Python; that's why we're here! :-)

And anyway, being nasty about other languages feels unPythonic to me...
From: Steven D'Aprano on
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(a)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
From: rantingrick on
On Aug 8, 8:15 pm, Steven D'Aprano <st...(a)REMOVE-THIS-
cybersource.com.au> wrote:

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

Devils Advocate!

PS: Man you're irb main was so full of cobweb i could barley see the
code... haa... haaaa... hachew!. ;-)
From: Steven D'Aprano on
On Mon, 09 Aug 2010 00:29:19 -0700, rantingrick wrote:

> On Aug 8, 8:15 pm, Steven D'Aprano <st...(a)REMOVE-THIS-
> cybersource.com.au> wrote:
>
>> 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.
>
> Devils Advocate!
>
> PS: Man you're irb main was so full of cobweb i could barley see the
> code... haa... haaaa... hachew!. ;-)

irb's default prompt is a bit too verbose for my tastes, but Python
allows you to customise its prompt too. You'll often see people here
posting copy/pastes with a customised prompt, so obviously some people
like that sort of thing.

Me, my biggest gripe with the interactive interpreter is that using >>>
as a prompt clashes with > as the standard quoting character in email and
news, but Guido has refused to even consider changing it.

And that it's quite finicky about blank lines between methods and inside
functions. Makes it hard to paste code directly into the interpreter.

And that pasting doesn't strip out any leading prompts. It needs a good
doctest mode.


--
Steven
From: Stefan Schwarzer on
Hi Steven,

On 2010-08-09 10:21, Steven D'Aprano wrote:
> And that it's quite finicky about blank lines between methods and inside
> functions. Makes it hard to paste code directly into the interpreter.
>
> And that pasting doesn't strip out any leading prompts. It needs a good
> doctest mode.

ipython [1] should help here:

IPython 0.10 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]: %paste?
Type: Magic function
Base Class: <type 'instancemethod'>
String Form: <bound method InteractiveShell.magic_paste of <IPython.iplib.InteractiveShell object at 0xb740096c>>
Namespace: IPython internal
File: /usr/lib/pymodules/python2.6/IPython/Magic.py
Definition: %paste(self, parameter_s='')
Docstring:
Allows you to paste & execute a pre-formatted code block from clipboard.

The text is pulled directly from the clipboard without user
intervention.

The block is dedented prior to execution to enable execution of method
definitions. '>' and '+' characters at the beginning of a line are
ignored, to allow pasting directly from e-mails, diff files and
doctests (the '...' continuation prompt is also stripped). The
executed block is also assigned to variable named 'pasted_block' for
later editing with '%edit pasted_block'.

You can also pass a variable name as an argument, e.g. '%paste foo'.
This assigns the pasted block to variable 'foo' as string, without
dedenting or executing it (preceding >>> and + is still stripped)

'%paste -r' re-executes the block previously entered by cpaste.

IPython statements (magics, shell escapes) are not supported (yet).

See also
--------
cpaste: manually paste code into terminal until you mark its end.

Unfortunatey, when I enter

In [2]: %paste

at the prompt it gives me (before I pasted anything)

In [2]: %paste
------------------------------------------------------------
File "<string>", line 1
http://pypi.python.org/pypi/ipython/0.10
^
SyntaxError: invalid syntax

So far, I couldn't find anything on the net on this.

[1] http://pypi.python.org/pypi/ipython

Stefan