ruby-dev (Japanese) list archive (unofficial mirror)
 help / color / mirror / Atom feed
From: merch-redmine@jeremyevans•net
To: ruby-dev@ruby-lang.org
Subject: [ruby-dev:50812] [Ruby master Feature#8853] Should String#sub(pattern) returns an Enumerator?
Date: Sat, 13 Jul 2019 00:32:36 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-79370.20190713003236.09bf8a16b4da1086@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-8853.20130903100808@ruby-lang.org

Issue #8853 has been updated by jeremyevans0 (Jeremy Evans).

Backport deleted (1.9.3: UNKNOWN, 2.0.0: UNKNOWN)
ruby -v deleted (ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux])
Tracker changed from Bug to Feature
File string-sub-enumerator.patch added

If I understand this correctly, this is a feature request to make `String#sub` and `#sub!` return an `Enumerator` if given a single argument and no block.  I'm not sure how useful this would be.  With `#sub!`, you can use the enumerator to mutate the string passing the replacement in via `Enumerator#feed`.  However, with `#sub`, you cannot use the enumerator to modify anything, since the `String#sub` receiver is not modified, and you can't get access to the resulting string.  However, assuming this is desired, the attached patch implements it.

----------------------------------------
Feature #8853: Should String#sub(pattern) returns an Enumerator?
https://bugs.ruby-lang.org/issues/8853#change-79370

* Author: okkez (okkez _)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
調べてみたのですが、過去に同様の議論があったのかどうかわからなかったので起票します。

以下のように String#sub(pattern) と使うと ArgumentError が発生します。

$ ruby -e "p ''.sub(//)"
-e:1:in `sub': wrong number of arguments (1 for 2) (ArgumentError)
        from -e:1:in `<main>'

一方、 String#gsub(pattern) だと Enumerator を返します。

$ ruby -e "p ''.gsub(//)"
#<Enumerator: "":gsub(//)>

RDoc ではこの挙動は説明されていませんでした。
String#gsub(pattern)がEnumeratorを返すことについては書かれています。
http://www.ruby-doc.org/core-2.0.0/String.html#method-i-sub
http://www.ruby-doc.org/core-2.0.0/String.html#method-i-gsub

一貫性の観点から考えると、String#sub(pattern)もEnumeratorを返した方が良いように思いましたがいかがでしょうか。
まあ、String#sub だと置換は一回しか発生しないのでイマイチな感じはします。

  * String#sub(pattern) が Enumerator を返すようになる
  * RDoc に String#gsub(pattern) と違って String#sub(pattern) が ArgumentError を発生させることの記述が追加される

のどちらかが、満たされればいいと思っています。

---Files--------------------------------
string-sub-enumerator.patch (3.52 KB)


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

      reply	other threads:[~2019-07-13  0:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-03  1:08 [ruby-dev:47688] [ruby-trunk - Bug #8853][Open] Should String#sub(pattern) returns an Enumerator? okkez (okkez _)
2019-07-13  0:32 ` merch-redmine [this message]

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-79370.20190713003236.09bf8a16b4da1086@ruby-lang.org \
    --to=ruby-dev@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
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).