From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: poffice@blade.nagaokaut.ac.jp Delivered-To: poffice@blade.nagaokaut.ac.jp Received: from kankan.nagaokaut.ac.jp (kankan.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (Postfix) with ESMTP id 43A2017C97BE for ; Fri, 27 Jul 2012 19:27:02 +0900 (JST) Received: from funfun.nagaokaut.ac.jp (smtp.nagaokaut.ac.jp [133.44.2.201]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 5212EEA6B2A for ; Fri, 27 Jul 2012 19:23:25 +0900 (JST) Received: from localhost (localhost.nagaokaut.ac.jp [127.0.0.1]) by funfun.nagaokaut.ac.jp (Postfix) with ESMTP id 34DD78FC1B for ; Fri, 27 Jul 2012 19:23:26 +0900 (JST) X-Virus-Scanned: amavisd-new at funfun.nagaokaut.ac.jp Received: from funfun.nagaokaut.ac.jp ([127.0.0.1]) by localhost (funfun.nagaokaut.ac.jp [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id WNQo+DHMibOM for ; Fri, 27 Jul 2012 19:23:26 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by funfun.nagaokaut.ac.jp (Postfix) with ESMTP id 11E798FC19 for ; Fri, 27 Jul 2012 19:23:26 +0900 (JST) Received: from carbon.ruby-lang.org (carbon.ruby-lang.org [221.186.184.68]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id B3A8095244C for ; Fri, 27 Jul 2012 19:23:22 +0900 (JST) Received: from beryllium.ruby-lang.org (beryllium.ruby-lang.org [127.0.0.1]) by carbon.ruby-lang.org (Postfix) with ESMTP id 105D53C21E2FE; Fri, 27 Jul 2012 19:23:21 +0900 (JST) Received: from fluorine.ruby-lang.org (www.rubyist.net [210.251.121.216]) by carbon.ruby-lang.org (Postfix) with ESMTP id 743E83C21F72F for ; Fri, 27 Jul 2012 19:23:20 +0900 (JST) Received: from ruby-lang.org (localhost [127.0.0.1]) by fluorine.ruby-lang.org (Postfix) with ESMTP id 4E81D3ED21 for ; Fri, 27 Jul 2012 19:23:20 +0900 (JST) Delivered-To: ruby-core@ruby-lang.org Date: Fri, 27 Jul 2012 19:23:20 +0900 Posted: Fri, 27 Jul 2012 19:23:20 +0900 From: "Eregon (Benoit Daloze)" Reply-To: ruby-core@ruby-lang.org Subject: [ruby-core:46807] [ruby-trunk - Feature #6801] String#~ for a here document To: ruby-core@ruby-lang.org Message-Id: References: X-ML-Name: ruby-core X-Mail-Count: 46807 X-MLServer: fml [fml 4.0.3 release (20011202/4.0.3)]; post only (only members can post) X-ML-Info: If you have a question, send e-mail with the body "help" (without quotes) to the address ruby-core-ctl@ruby-lang.org; help= X-Mailer: Redmine X-Redmine-Issue-Author: merborne X-Auto-Response-Suppress: OOF X-Redmine-Issue-Id: 6801 X-Redmine-Mailinglistintegration-Message-Ids: 17169 X-Redmine-Project: ruby-trunk Auto-Submitted: auto-generated X-Redmine-Site: Ruby Issue Tracking System X-Redmine-Host: bugs.ruby-lang.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Precedence: bulk List-Id: ruby-core.ruby-lang.org List-Software: fml [fml 4.0.3 release (20011202/4.0.3)] List-Post: List-Owner: List-Help: List-Unsubscribe: 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. (()) =end -- http://bugs.ruby-lang.org/