Ruby-dev summary 19198-19345

it has one simple good reason for use: it allows good form indentation of code
while conserving storage. all those extra spaces add up. (for example say, in
HTML gerneration).

···

On Tuesday 14 January 2003 09:08 pm, Yukihiro Matsumoto wrote:

Hi,

In message “Re: ruby-dev summary 19198-19345” > > on 03/01/15, Paul Brannan pbrannan@atdesk.com writes:

Note that the perl community has already given this problem much
thought:
GitHub - Raku/old-design-docs: Raku language design documents

In message “Re: ruby-dev summary 19198-19345” > > on 03/01/15, Matt Armstrong matt@lickey.com writes:

I do not like this simply because it introduces a problem of tabs -vs-
spaces.

I’m not sure Perl5 RFC162 is well though or not. But Matt’s comment
is exactly what I thought when I read that. Nobu’s idea is much
better, although I’m not convinced yet.

  					matz.


tom sawyer, aka transami
transami@transami.net

                               .''.
   .''.      .        *''*    :_\/_:     .
  :_\/_:   _\(/_  .:.*_\/_*   : /\ :  .'.:.'.

.‘’.: /\ : ./)\ ‘:’* /\ * : ‘…’. -=:o:=-
:/:‘.:::. | ’ ‘’ * ‘.'/.’ (/’.‘:’.’
: /\ : ::::: = / -= o =- /)\ ’ *
‘…’ ‘:::’ === * /\ * .‘/.'. ‘._____
* | : |. |’ .—"|
* | _ .–’| || | _| |
* | .-‘| __ | | | || |
.-----. | |’ | || | | | | | || |
__’ ’ /“\ | '-.”". ‘-’ ‘-.’ '` |.

Gavin Sinclair wrote:

One difference between this and a here-doc is the initial newline,
which is a shame.

Yes, but the following gets to be idiomatic after a while:

str = %{
first line
second line
}

sorry, it was Gavin. Gavin done did it! now i’m really creeped-out by those
here documents. :slight_smile:

···

On Wednesday 15 January 2003 10:59 pm, Hal E. Fulton wrote:

Hmm, I think that was someone else.

Hal


tom sawyer, aka transami
transami@transami.net

                               .''.
   .''.      .        *''*    :_\/_:     .
  :_\/_:   _\(/_  .:.*_\/_*   : /\ :  .'.:.'.

.‘’.: /\ : ./)\ ‘:’* /\ * : ‘…’. -=:o:=-
:/:‘.:::. | ’ ‘’ * ‘.'/.’ (/’.‘:’.’
: /\ : ::::: = / -= o =- /)\ ’ *
‘…’ ‘:::’ === * /\ * .‘/.'. ‘._____
* | : |. |’ .—"|
* | _ .–’| || | _| |
* | .-‘| __ | | | || |
.-----. | |’ | || | | | | | || |
__’ ’ /“\ | '-.”". ‘-’ ‘-.’ '` |.

very cool Gavin, unfortunately i can’t use that since i have all my tabs
converted to spaces by my editor. is there a spaces version perhaps?

···

On Wednesday 15 January 2003 10:08 pm, Gavin Sinclair wrote:

s = %Q{
hi
how are you?
}.tabto(0)


tom sawyer, aka transami
transami@transami.net

                               .''.
   .''.      .        *''*    :_\/_:     .
  :_\/_:   _\(/_  .:.*_\/_*   : /\ :  .'.:.'.

.‘’.: /\ : ./)\ ‘:’* /\ * : ‘…’. -=:o:=-
:/:‘.:::. | ’ ‘’ * ‘.'/.’ (/’.‘:’.’
: /\ : ::::: = / -= o =- /)\ ’ *
‘…’ ‘:::’ === * /\ * .‘/.'. ‘._____
* | : |. |’ .—"|
* | _ .–’| || | _| |
* | .-‘| __ | | | || |
.-----. | |’ | || | | | | | || |
__’ ’ /“\ | '-.”". ‘-’ ‘-.’ '` |.

Hi,

I think I might start using that more often. It’s nice, and it’s
easier to chain. e.g. with an appropriate String#tabto(n) method (a
la StringFormattingFunctions on the Wiki), you can resolve the
indentation like so:

s = %Q{
hi
how are you?
}.tabto(0)

Which gives “\nhi\n how are you?\n”. That code would highlight fine,
whereas using here-docs can sometimes be troublesome.

One difference between this and a here-doc is the initial newline,
which is a shame.

One more difference. tabto strips after string interpolcation,
whereas here-doc does before it.

hi = " hi"
s0 = %q{
#{hi}
how are you?
}.tabto(0)

=> “\nhi\nhow are you?\n”

s1 = <<|EOS
>#{hi}
> how are you?
EOS

=> “\n hi\n how are you?\n”

···

At Thu, 16 Jan 2003 14:08:58 +0900, Gavin Sinclair wrote:


Nobu Nakada

Note also that if the text you are printing includes a } character, this
won’t work. You’ll have to use %Q instead or %Q%some string% (or some
variant) instead,

Well, only of the {}'s are unbalanced. For Example …

$ irb --simple-prompt

s = %Q{ this is { just a } test }
=> " this is { just a } test "

···

On Thu, 2003-01-16 at 09:58, Paul Brannan wrote:


– Jim Weirich jweirich@one.net http://w3.one.net/~jweirich

“Beware of bugs in the above code; I have only proved it correct,
not tried it.” – Donald Knuth (in a memo to Peter van Emde Boas)

I like it, too.

I think I like the Perl way better, but either way is nicer than none at
all.

:slight_smile:

Chris

···

----- Original Message -----
From: “Daniel Carrera” dcarrera@math.umd.edu
Newsgroups: comp.lang.ruby
Sent: Tuesday, January 14, 2003 6:56 PM
Subject: Re: ruby-dev summary 19198-19345

Come to think of it, this feature is really useful. I’d like to see it
implemented.

Cheers,
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

On Wed, 15 Jan 2003, Gavin Sinclair wrote:

On Wednesday, January 15, 2003, 12:36:43 PM, Daniel wrote:

On Wed, 15 Jan 2003, Gavin Sinclair wrote:

p <<-|EOS
  >  foo
  >    bar
EOS
# => "  foo\n    bar\n".

[snip]

Anyway. I want this functionality so bad I can taste it.

Gavin, maybe I’m missing something but I’m not sure I understand the
usefulness of this. The only example I can think of where I might like
this is when I have lots of indentation:

def function
if (condition)
p <<-|EOH
>Blah blah blah
>Blah blah blah
EOH
end
end

Well, that’s a good example! Here-documents can come up anywhere.

Is this the purpose of this feature? Or am I missing something?

Even with little/no indentation, the proposed feature would be useful
to me. I usually indent code in posts, but here will put is as if it
were in a Ruby program.

USAGE =<<-|EOU

Usage: froboz [-q] [-o DIR] files…
-q: quiet operation
-o: output to DIR

(c) 2003 Widgets Co
EOU

--------- vs ---------

USAGE =<<-EOU
Usage: froboz [-q] [-o DIR] files…
-q: quiet operation
-o: output to DIR

(c) 2003 Widgets Co
EOU

Being able to indent you text gives the same benefit as all
indentation: highlights code structure.

Gavin

matz@ruby-lang.org (Yukihiro Matsumoto) writes:

I do not like this simply because it introduces a problem of tabs -vs-
spaces.

I’m not sure Perl5 RFC162 is well though or not. But Matt’s comment
is exactly what I thought when I read that. Nobu’s idea is much
better, although I’m not convinced yet.

Why are you not convinced, Matz? YAGNI? Or implementation difficulty?
I think it’s a very good solution to a very real problem. The problem
is only aesthetic, but no less real for that.

If I were implementing it, I’d limit the “special character” to ‘|’,
unless someone demonstrated a reason why this shouldn’t be the case.

What I do not like about Nobu’s idea is that the here document may be
harder to edit, since every line must have the leading | character.
The extra editing pain may outweigh any benefit.

Editing this in Vim would be easier than slipping on a banana peel.
Surely this must also be the case with Emacs?

And the worst that can be said about the feature is this: if the
drawback (editing) outweighs the advantage (which is subjective, but
to some people, great), then you don’t have to use it.

Gavin

···

On Wednesday, January 15, 2003, 3:17:11 PM, Matt wrote:

In message “Re: ruby-dev summary 19198-19345” >> on 03/01/15, Matt Armstrong matt@lickey.com writes:

nice, never thought of that. then again you can also use the first line too:

str = %Q{first line
second line}

···

On Wednesday 15 January 2003 11:10 pm, Joel VanderWerf wrote:

Gavin Sinclair wrote:

One difference between this and a here-doc is the initial newline,
which is a shame.

Yes, but the following gets to be idiomatic after a while:

str = %{
first line
second line
}


tom sawyer, aka transami
transami@transami.net

                               .''.
   .''.      .        *''*    :_\/_:     .
  :_\/_:   _\(/_  .:.*_\/_*   : /\ :  .'.:.'.

.‘’.: /\ : ./)\ ‘:’* /\ * : ‘…’. -=:o:=-
:/:‘.:::. | ’ ‘’ * ‘.'/.’ (/’.‘:’.’
: /\ : ::::: = / -= o =- /)\ ’ *
‘…’ ‘:::’ === * /\ * .‘/.'. ‘._____
* | : |. |’ .—"|
* | _ .–’| || | _| |
* | .-‘| __ | | | || |
.-----. | |’ | || | | | | | || |
__’ ’ /“\ | '-.”". ‘-’ ‘-.’ '` |.

Tom Sawyer wrote:

···

On Wednesday 15 January 2003 10:08 pm, Gavin Sinclair wrote:

s = %Q{
hi
how are you?
}.tabto(0)

very cool Gavin, unfortunately i can’t use that since i have all my tabs
converted to spaces by my editor. is there a spaces version perhaps?

(untested)

.sub!(/^ *//)

would do the trick yes?

cheers
dim

Hi,

···

In message “Re: ruby-dev summary 19198-19345” on 03/01/15, Gavin Sinclair gsinclair@soyabean.com.au writes:

Why are you not convinced, Matz? YAGNI? Or implementation difficulty?
I think it’s a very good solution to a very real problem. The problem
is only aesthetic, but no less real for that.

Nobu already made a patch to implement it. I feel like it can make
programs more cryptic. “<<-|FOO”?? WHAT DOES IT MEAN?

Other heredoc notations follow shell convention, so that there’s
nothing new in syntax. One of Ruby’s secret is to be slow to merge
new syntax.

						matz.

Dmitri Colebatch wrote:

Tom Sawyer wrote:

s = %Q{
hi
how are you?
}.tabto(0)

very cool Gavin, unfortunately i can’t use that since i have all my
tabs converted to spaces by my editor. is there a spaces version perhaps?

It is the spaces version, just with a bad name. I use spaces to emulate
hard tabs, too.

(untested)

.sub!(/^ *//)

would do the trick yes?

Nope, #tabto preserves relative indentation of lines. I probably should
have called it something else, like “indent_to” or “indent_block”.

If anyone has a better name, please put add it to:

···

On Wednesday 15 January 2003 10:08 pm, Gavin Sinclair wrote:

Yukihiro Matsumoto wrote:

Hi,

Why are you not convinced, Matz? YAGNI? Or implementation difficulty?
I think it’s a very good solution to a very real problem. The problem
is only aesthetic, but no less real for that.

Nobu already made a patch to implement it. I feel like it can make
programs more cryptic. “<<-|FOO”?? WHAT DOES IT MEAN?

Other heredoc notations follow shell convention, so that there’s
nothing new in syntax. One of Ruby’s secret is to be slow to merge
new syntax.

  					matz.

It’s a great secret. Keep the syntax simple, the core classes clean, and
let library developers fill in minor details like this.

This works well enough for me, and I don’t have to type | characters.
(But it doesn’t solve the hard-tabs-versus-spaces problem; I just don’t
use hard tabs.)

class String

# Tabs left or right by n chars, using spaces
def tab(n)
  if n >= 0
    gsub(/^/, ' ' * n)
  else
    gsub(/^ {0,#{-n}}/, "")
  end
end

# Preserves relative tabbing.
# The first non-empty line ends up with n spaces before nonspace.
def tabto(n)
  if self =~ /^( *)\S/
    tab(n - $1.length)
  else
    self
  end
end

end

   str = %{\
     header
       subhead
         text
   }.tabto(2)

puts str

===output===
header
subhead
text

···

In message “Re: ruby-dev summary 19198-19345” > on 03/01/15, Gavin Sinclair gsinclair@soyabean.com.au writes:

When I first saw it mentioned in a ruby-dev summary several weeks ago,
I knew instantly what it meant. You see the | representing a left
margin, and it’s obvious.

The secret is already violated with regard to <<-EOF (i.e. allowing an
indented end marker). Well, I just tried it in bash, and it didn’t
allow an indented EOF. Maybe other sheels do.

Gavin

···

On Wednesday, January 15, 2003, 4:39:36 PM, Yukihiro wrote:

Hi,

In message “Re: ruby-dev summary 19198-19345” > on 03/01/15, Gavin Sinclair gsinclair@soyabean.com.au writes:

Why are you not convinced, Matz? YAGNI? Or implementation difficulty?
I think it’s a very good solution to a very real problem. The problem
is only aesthetic, but no less real for that.

Nobu already made a patch to implement it. I feel like it can make
programs more cryptic. “<<-|FOO”?? WHAT DOES IT MEAN?

Other heredoc notations follow shell convention, so that there’s
nothing new in syntax. One of Ruby’s secret is to be slow to merge
new syntax.

Hi,

Why are you not convinced, Matz? YAGNI? Or implementation difficulty?
I think it’s a very good solution to a very real problem. The problem
is only aesthetic, but no less real for that.

Nobu already made a patch to implement it. I feel like it can make
programs more cryptic. “<<-|FOO”?? WHAT DOES IT MEAN?

If you’re not used to it, even <<-FOO is cryptic.

Personally, I’d dispense with the hyphen in this
case… only one punctuation mark is needed
in order to remove ambiguity: <<|FOO

Other heredoc notations follow shell convention, so that there’s
nothing new in syntax. One of Ruby’s secret is to be slow to merge
new syntax.

We all trust your judgment, as you have treated us
very well in the past. :slight_smile: A (mineral) ruby is not
produced quickly, nor is a (software) Ruby.

Hal

···

----- Original Message -----
From: “Yukihiro Matsumoto” matz@ruby-lang.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, January 14, 2003 11:39 PM
Subject: Re: ruby-dev summary 19198-19345

In message “Re: ruby-dev summary 19198-19345” > on 03/01/15, Gavin Sinclair gsinclair@soyabean.com.au writes:

matz@ruby-lang.org (Yukihiro Matsumoto) writes:

Nobu already made a patch to implement it. I feel like it can make
programs more cryptic. “<<-|FOO”?? WHAT DOES IT MEAN?

I just though, why not “<<|FOO”. In other words, the ‘-’ in there
doesn’t serve much purpose.

s = <<|FOO
>this
> is
> my
> text
FOO

It is the spaces version, just with a bad name. I use spaces to emulate
hard tabs, too.

great! i love it!

http://www.rubygarden.org/ruby?StringFormattingFunctions

i lloked these over. mind if i add them to my general purpose library
(tomslib/rubylib?)

i thought a tad on the names to, i think they’re mostly fine, but seems to me
it might be a touch better if they were:

indent(n)
indent_to(n)
indent_all(n)

and then with aliases of:

tab(n)
tab_to(n)
tab_all(n)

···

On Thursday 16 January 2003 01:08 am, Joel VanderWerf wrote:


tom sawyer, aka transami
transami@transami.net

                               .''.
   .''.      .        *''*    :_\/_:     .
  :_\/_:   _\(/_  .:.*_\/_*   : /\ :  .'.:.'.

.‘’.: /\ : ./)\ ‘:’* /\ * : ‘…’. -=:o:=-
:/:‘.:::. | ’ ‘’ * ‘.'/.’ (/’.‘:’.’
: /\ : ::::: = / -= o =- /)\ ’ *
‘…’ ‘:::’ === * /\ * .‘/.'. ‘._____
* | : |. |’ .—"|
* | _ .–’| || | _| |
* | .-‘| __ | | | || |
.-----. | |’ | || | | | | | || |
__’ ’ /“\ | '-.”". ‘-’ ‘-.’ '` |.

Hi,

When I first saw it mentioned in a ruby-dev summary several weeks ago,
I knew instantly what it meant. You see the | representing a left
margin, and it’s obvious.

I don’t forget there’s one feel like that. Thank you.

The secret is already violated with regard to <<-EOF (i.e. allowing an
indented end marker). Well, I just tried it in bash, and it didn’t
allow an indented EOF. Maybe other sheels do.

sh has “<<-EOF”.

						matz.
···

In message “Re: ruby-dev summary 19198-19345” on 03/01/15, Gavin Sinclair gsinclair@soyabean.com.au writes:

matz@ruby-lang.org (Yukihiro Matsumoto) writes:

Nobu already made a patch to implement it. I feel like it can make
programs more cryptic. “<<-|FOO”?? WHAT DOES IT MEAN?

I just though, why not “<<|FOO”. In other words, the ‘-’ in there
doesn’t serve much purpose.

s = <<|FOO
>this
> is
> my
> text
FOO

To me, the ‘-’ means the same that it always does: it allows the
end-of-document marker to be indented. I don’t see why that should
change. In other words, the example given above is an incomplete
here-document, based on the assumption in the following section.

What happens if a here-doc is specified to have the left margin (‘|’)
but for one or more lines it is not used, as in:

s = <<-|EOS
> One
> Two
Three
> Four
EOS

puts s

I think the correct output is:
One
Two
Three
Four
(end of output)

In that case, it’s pretty easy to see what becomes of the quoted
example at the top. The fifth line is " FOO".

At the end of the day, the “-” and (proposed) “|” features of Ruby
here-documents are independent of each other.

Gavin

···

On Thursday, January 16, 2003, 2:39:54 AM, Matt wrote:

Matt Armstrong wrote:

I just though, why not “<<|FOO”. In other words, the ‘-’ in there
doesn’t serve much purpose.

s = <<|FOO
>this
> is
> my
> text
FOO

In fact, neither does the ‘FOO’

s  = <<|Now is the time
       > For all good men
       >    to code Ruby
p s.length

So any line that doesn’t start with the given delimiter (ignoring
leading whitespace) ends the string.

Cheers

Dave