ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "merborne (kyo endo)" <redmine@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:46812] [ruby-trunk - Feature #6801] String#~ for a here document
Date: Fri, 27 Jul 2012 22:38:14 +0900	[thread overview]
Message-ID: <redmine.journal-28486.20120727223813@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-6801.20120727131625@ruby-lang.org


Issue #6801 has been updated by merborne (kyo endo).


Eregon (Benoit Daloze) wrote:
> 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.

Heredoc sytax is already cryptic. so programmers looks it carefully and find the difference between <<-EOS and ~<<-EOS. Yes, this might be supported in the parser. But if so, it will be a big feature or big issue and might be difficult to find the exit. I don't know. On the other hand, I don't prefer Ruby provide <<-EOS.undent style method for this purpose. I prefer it looks a special syntax same as - or <<.

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

Sorry, I don't understand your point. In the example, the target string is shifted to the left for 4 whitespaces with String#~.

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

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/

  parent reply	other threads:[~2012-07-27 13:41 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-27  4:16 [ruby-core:46799] [ruby-trunk - Feature #6801][Open] String#~ for a here document merborne (kyo endo)
2012-07-27  5:19 ` [ruby-core:46800] [ruby-trunk - Feature #6801] " nobu (Nobuyoshi Nakada)
2012-07-27 10:23 ` [ruby-core:46807] " Eregon (Benoit Daloze)
2012-07-27 13:38 ` merborne (kyo endo) [this message]
2012-07-27 14:03 ` [ruby-core:46815] " Eregon (Benoit Daloze)
2012-07-27 14:38 ` [ruby-core:46816] " merborne (kyo endo)
2012-07-27 16:52 ` [ruby-core:46820] " trans (Thomas Sawyer)
2012-10-03  9:25 ` [ruby-core:47814] " fahmisetiawand (Fahmi Setiawan)
2012-11-24  0:12 ` [ruby-core:49925] [ruby-trunk - Feature #6801][Assigned] " mame (Yusuke Endoh)
2012-11-24  3:12 ` [ruby-core:49988] [ruby-trunk - Feature #6801] " trans (Thomas Sawyer)
2019-01-16 17:14 ` [ruby-core:91130] [Ruby trunk Feature#6801] " justin
2019-01-16 20:49 ` [ruby-core:91132] " eregontp

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-28486.20120727223813@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).