String#split with a block
I would like
String#split to take an optional block, similar to
String#scan with a block.
Suppose I have a regex pattern with captures, and I want to iterate over matches while having access to the information in last match
$~. In such case, I can use
scan, and access the last match in the block during iteration:
"abcabd".scan(/(a)|(b)/) do |s| if $1 then ... # `s` is `"a"`, `"a"` elsif $2 then ... # `s` is `"b"`, `"b"` end end
In my use case, I need to iterate over not only the matching patterns, but also the non-matched substrings in between them. For such cases, I need to use
split instead of
scan. And I would like to do something like the following:
"abcabd".split(/((a)|(b))/) do |s| if $2 then ... # `s` is `"a"`, `"a"` elsif $3 then ... # `s` is `"b"`, `"b"` else ... # `s` is `"c"`, `"d"`, and `$~` should be `nil` end end
With the current functionalities, I attempted to do this:
"abcabd".split(/((a)|(b))/).each do ... end
but it does not work the way I wanted; within the
each block, the last match value
nil, and I cannot access the information.
Updated by shyouhei (Shyouhei Urabe) almost 5 years ago
- Status changed from Open to Feedback
There are methods like String.each_char.slice_when (and friends). See http://ruby-doc.org/core-2.3.0/Enumerable.html#method-i-slice_when
Doesn't this solve your problem? Then, can you show us a bit more details about your use case?