ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:69743] [Ruby trunk - Feature #11309] [Open] Iterator over string matches
       [not found] <redmine.issue-11309.20150626145543@ruby-lang.org>
@ 2015-06-26 14:55 ` sawadatsuyoshi
  2015-06-26 14:58 ` [ruby-core:69744] [Ruby trunk - Feature #11309] " sawadatsuyoshi
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 4+ messages in thread
From: sawadatsuyoshi @ 2015-06-26 14:55 UTC (permalink / raw)
  To: ruby-core

Issue #11309 has been reported by Tsuyoshi Sawada.

----------------------------------------
Feature #11309: Iterator over string matches
https://bugs.ruby-lang.org/issues/11309

* Author: Tsuyoshi Sawada
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
This was hinted from a problem in stackoverflow (http://stackoverflow.com/questions/31074050/build-list-of-strings-containing-substrings-separated-by-an-from-a-string/31075511#31075511).

Suppose there is a string:

    s = "a_b_c_d_e"

To get an array of pre-matches that result from matching `s` with `"_"`, I can do this:

    a = []
    s.scan("_"){a.push($`)}
    a # => ["a", "a_b", "a_b_c", "a_b_c_d"]

But this looks too Perlish. I thought it would be nice if there is a method on `String` that creates an enumerator over matches so that I can do something like this:

    "a_b_c_d_e".some_method("_").with_object([]){|m, a| a.push(m.post_match)}
    # => ["a", "a_b", "a_b_c", "a_b_c_d"]

where `m` is the last matchdata instance at that point. I believe such method would have wider application.



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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:69744] [Ruby trunk - Feature #11309] Iterator over string matches
       [not found] <redmine.issue-11309.20150626145543@ruby-lang.org>
  2015-06-26 14:55 ` [ruby-core:69743] [Ruby trunk - Feature #11309] [Open] Iterator over string matches sawadatsuyoshi
@ 2015-06-26 14:58 ` sawadatsuyoshi
  2015-07-01  1:25 ` [ruby-core:69822] " duerst
  2015-07-01  8:35 ` [ruby-core:69824] " sawadatsuyoshi
  3 siblings, 0 replies; 4+ messages in thread
From: sawadatsuyoshi @ 2015-06-26 14:58 UTC (permalink / raw)
  To: ruby-core

Issue #11309 has been updated by Tsuyoshi Sawada.


Oops, I meant `pre_match`, not `post_match`. Sorry.

----------------------------------------
Feature #11309: Iterator over string matches
https://bugs.ruby-lang.org/issues/11309#change-53125

* Author: Tsuyoshi Sawada
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
This was hinted from a problem in stackoverflow (http://stackoverflow.com/questions/31074050/build-list-of-strings-containing-substrings-separated-by-an-from-a-string/31075511#31075511).

Suppose there is a string:

    s = "a_b_c_d_e"

To get an array of pre-matches that result from matching `s` with `"_"`, I can do this:

    a = []
    s.scan("_"){a.push($`)}
    a # => ["a", "a_b", "a_b_c", "a_b_c_d"]

But this looks too Perlish. I thought it would be nice if there is a method on `String` that creates an enumerator over matches so that I can do something like this:

    "a_b_c_d_e".some_method("_").with_object([]){|m, a| a.push(m.post_match)}
    # => ["a", "a_b", "a_b_c", "a_b_c_d"]

where `m` is the last matchdata instance at that point. I believe such method would have wider application.



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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:69822] [Ruby trunk - Feature #11309] Iterator over string matches
       [not found] <redmine.issue-11309.20150626145543@ruby-lang.org>
  2015-06-26 14:55 ` [ruby-core:69743] [Ruby trunk - Feature #11309] [Open] Iterator over string matches sawadatsuyoshi
  2015-06-26 14:58 ` [ruby-core:69744] [Ruby trunk - Feature #11309] " sawadatsuyoshi
@ 2015-07-01  1:25 ` duerst
  2015-07-01  8:35 ` [ruby-core:69824] " sawadatsuyoshi
  3 siblings, 0 replies; 4+ messages in thread
From: duerst @ 2015-07-01  1:25 UTC (permalink / raw)
  To: ruby-core

Issue #11309 has been updated by Martin Dürst.


What do you think looks too Perlish? Is it just the $`?

In that case, having something like $MATCH (as an alias to $~) might help:

"a_b_c_d_e".scan("_").with_object([]) { |_, a| a.push $MATCH.post_match }

Even without that,

"a_b_c_d_e".scan("_").with_object([]) { |_, a| a.push $~.post_match }

is quite readable, because "post_match" makes it easy to understand that $~ must be the current match.


----------------------------------------
Feature #11309: Iterator over string matches
https://bugs.ruby-lang.org/issues/11309#change-53220

* Author: Tsuyoshi Sawada
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
This was hinted from a problem in stackoverflow (http://stackoverflow.com/questions/31074050/build-list-of-strings-containing-substrings-separated-by-an-from-a-string/31075511#31075511).

Suppose there is a string:

    s = "a_b_c_d_e"

To get an array of pre-matches that result from matching `s` with `"_"`, I can do this:

    a = []
    s.scan("_"){a.push($`)}
    a # => ["a", "a_b", "a_b_c", "a_b_c_d"]

But this looks too Perlish. I thought it would be nice if there is a method on `String` that creates an enumerator over matches so that I can do something like this:

    "a_b_c_d_e".some_method("_").with_object([]){|m, a| a.push(m.post_match)}
    # => ["a", "a_b", "a_b_c", "a_b_c_d"]

where `m` is the last matchdata instance at that point. I believe such method would have wider application.



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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:69824] [Ruby trunk - Feature #11309] Iterator over string matches
       [not found] <redmine.issue-11309.20150626145543@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2015-07-01  1:25 ` [ruby-core:69822] " duerst
@ 2015-07-01  8:35 ` sawadatsuyoshi
  3 siblings, 0 replies; 4+ messages in thread
From: sawadatsuyoshi @ 2015-07-01  8:35 UTC (permalink / raw)
  To: ruby-core

Issue #11309 has been updated by Tsuyoshi Sawada.


Martin Dürst wrote:
> What do you think looks too Perlish? Is it just the $`?

That is one. But a more severe one is that (I thought) I had to initialize `a = []` in a separate line.

> "a_b_c_d_e".scan("_").with_object([]) { |_, a| a.push $MATCH.post_match }
> "a_b_c_d_e".scan("_").with_object([]) { |_, a| a.push $~.post_match }

These are better. But I still don't like the fact that I have to access a global variable.

----------------------------------------
Feature #11309: Iterator over string matches
https://bugs.ruby-lang.org/issues/11309#change-53223

* Author: Tsuyoshi Sawada
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
This was hinted from a problem in stackoverflow (http://stackoverflow.com/questions/31074050/build-list-of-strings-containing-substrings-separated-by-an-from-a-string/31075511#31075511).

Suppose there is a string:

    s = "a_b_c_d_e"

To get an array of pre-matches that result from matching `s` with `"_"`, I can do this:

    a = []
    s.scan("_"){a.push($`)}
    a # => ["a", "a_b", "a_b_c", "a_b_c_d"]

But this looks too Perlish. I thought it would be nice if there is a method on `String` that creates an enumerator over matches so that I can do something like this:

    "a_b_c_d_e".some_method("_").with_object([]){|m, a| a.push(m.post_match)}
    # => ["a", "a_b", "a_b_c", "a_b_c_d"]

where `m` is the last matchdata instance at that point. I believe such method would have wider application.



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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-07-01  8:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-11309.20150626145543@ruby-lang.org>
2015-06-26 14:55 ` [ruby-core:69743] [Ruby trunk - Feature #11309] [Open] Iterator over string matches sawadatsuyoshi
2015-06-26 14:58 ` [ruby-core:69744] [Ruby trunk - Feature #11309] " sawadatsuyoshi
2015-07-01  1:25 ` [ruby-core:69822] " duerst
2015-07-01  8:35 ` [ruby-core:69824] " sawadatsuyoshi

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