From: "Eregon (Benoit Daloze)" <redmine@...>
Date: 2012-07-27T19:23:20+09:00
Subject: [ruby-core:46807] [ruby-trunk - Feature #6801] String#~ for a here document


Issue #6801 has been updated by Eregon (Benoit Daloze).


I think the sequence ~<<-EOS becomes very cryptic, and this should be supported in the parser (or at least be integrated into the language). I actually prefer <<-EOS.undent as the intention is clearer.

Maybe something like <<+EOS, which would remove the common indentation?
In your example, it seems fine to have to manually undent the text,
but I think the most common use-case is declaring an heredoc in an indented code.
It does not look nice to have to lose all indentation at that place.
It seems I have similar ideas to what was said in [ruby-core:39851]
(although I don't want explicit indent with '|', I'd still prefer #undent or #~ in that case).

----------------------------------------
Feature #6801: String#~ for a here document
https://bugs.ruby-lang.org/issues/6801#change-28481

Author: merborne (kyo endo)
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 


=begin
Let me propose a new method (({String#~})) for a here document.

������������������������(({String#~}))���������������������

    class String
      def ~
        margin = scan(/^ +/).map(&:size).min
        gsub(/^ {#{margin}}/, '')
      end
    end

This is for removing leading margins of a here document.

������������������������������������������������������������������������������������������������������

    class ATool
      def self.help
        lines = ~<<-EOS
                  Instruction of `#{self}`
    
        `#{self}` is one of a great tool in the world.
           This helps you a lot on your daily work.
          Your life will be changed with `#{self}`!!
               Everyone knows about `#{self}`.
            So, You can ask them to learn `#{self}`
    
                    Just Use `#{self}`
    
                       from Today!
        EOS
        lines
      end
    end
    
    puts ATool.help
    
    # >>           Instruction of `ATool`
    # >> 
    # >> `ATool` is one of a great tool in the world.
    # >>    This helps you a lot on your daily work.
    # >>   Your life will be changed with `ATool`!!
    # >>        Everyone knows about `ATool`.
    # >>     So, You can ask them to learn `ATool`
    # >> 
    # >>             Just Use `ATool`
    # >> 
    # >>                from Today!

you can put a tilde sign just before the "<<" characters to call String#~, just like putting a minus sign after "<<" to indent the terminator. This is achieved with uniqueness of tilde sign method, which takes the receiver object on the right-hand side.

If you define String#unindent for this purpose, you put `.unindent` after EOS. I think its less elegant than a tilde.

"<<"������������������������������������������������String#~������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������String#unindent���������������������������EOS���������`.unindent`������������������������������������������������������������������������������������������������

I understand that this is not general method for string but just for here document. However, I think using a tilde as a method is very restricted because it can't take parameters and is difficult to read the meanings or behavior from the sign. From this, there are few tilde methods within built-in classes of ruby inspite of its uniqness(only for Fixnum, Bignum and Regexp).

so, I think using tilde for the above purpose is the one of the few good chances.

Thank you for your consideration.

���������������������String���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Ruby���������������������������������������������������������Fixnum, Bignum���������Regexp���������������

������������������������������������������������������������������������������������������������������������

���������������������������������������������������������������

This is based on the following my Japanese blog post.

((<URL:http://melborne.github.com/2012/04/27/ruby-heredoc-without-leading-whitespace/>))
=end



-- 
http://bugs.ruby-lang.org/