[#90399] [Ruby trunk Feature#14813] [PATCH] gc.c: make gc_enter+gc_exit pairs dtrace probes, too — ko1@...
Issue #14813 has been updated by ko1 (Koichi Sasada).
3 messages
2018/12/10
[#90417] [Ruby trunk Bug#15398] TestThread#test_signal_at_join fails on FreeBSD — naruse@...
Issue #15398 has been reported by naruse (Yui NARUSE).
4 messages
2018/12/11
[#90423] Re: [Ruby trunk Bug#15398] TestThread#test_signal_at_join fails on FreeBSD
— Eric Wong <normalperson@...>
2018/12/11
naruse@airemix.jp wrote:
[#90519] Spoofing warnings for mail from bugs.ruby-lang.org — Charles Oliver Nutter <headius@...>
I'm getting a spoofing warning for emails sent from bugs.ruby-lang.org when
4 messages
2018/12/13
[#90522] Re: Spoofing warnings for mail from bugs.ruby-lang.org
— Eric Wong <normalperson@...>
2018/12/13
Charles Oliver Nutter <headius@headius.com> wrote:
[#90533] [Ruby trunk Feature#15413] unmarkable C stack (3rd stack) — normalperson@...
Issue #15413 has been reported by normalperson (Eric Wong).
3 messages
2018/12/14
[#90581] [Ruby trunk Bug#15424] Ruby 2.6.0rc1 & 2.6.0rc2 mutex exception — mat999@...
Issue #15424 has been reported by splitice (Mathew Heard).
3 messages
2018/12/17
[#90595] [Ruby trunk Bug#15430] test_fork_while_parent_locked is failing status on Ruby CI — hsbt@...
Issue #15430 has been reported by hsbt (Hiroshi SHIBATA).
3 messages
2018/12/18
[#90614] [Ruby trunk Bug#15430][Assigned] test_fork_while_parent_locked is failing status on Ruby CI — hsbt@...
Issue #15430 has been updated by hsbt (Hiroshi SHIBATA).
4 messages
2018/12/19
[#90630] Re: [Ruby trunk Bug#15430][Assigned] test_fork_while_parent_locked is failing status on Ruby CI
— Eric Wong <normalperson@...>
2018/12/20
> It still exists. https://rubyci.org/logs/rubyci.s3.amazonaws.com/centos7/ruby-trunk/log/20181218T230003Z.fail.html.gz
[#90820] Re: [ruby-cvs:73697] k0kubun:r66593 (trunk): accept_nonblock_spec.rb: skip spurious failure — Eric Wong <normalperson@...>
k0kubun@ruby-lang.org wrote:
3 messages
2018/12/30
[ruby-core:90666] [Ruby trunk Bug#15450] Add a method `String#each_match` to the Ruby core
From:
cary@...
Date:
2018-12-21 19:25:36 UTC
List:
ruby-core #90666
Issue #15450 has been reported by CaryInVictoria (Cary Swoveland).
----------------------------------------
Bug #15450: Add a method `String#each_match` to the Ruby core
https://bugs.ruby-lang.org/issues/15450
* Author: CaryInVictoria (Cary Swoveland)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
* ruby -v:
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
`String#each_match` would have two forms:
*each_match(pattern) { |match| block } → str*
*each_match(pattern) → an_enumerator*
The latter would be identical to the form *gsub(pattern) → enumerator* of [String#gsub](http://ruby-doc.org/core-2.5.1/String.html#method-i-gsub). The former would simply yield the matches to a block and return the receiver.
I frequently use the form of `gsub` that returns an enumerator instead of `scan` when chaining to Enumerable methods. That's because `scan` returns an unneeded temporary array. This use of `gsub` can also be useful when the pattern contains capture groups, which can be a complication when using `scan`, as in the following example
Suppose we are given a string and wish to count the number of occurrences of each word that begins and ends with the same letter (case-insensitive).
str = "Viv and Bob are party animals. Bob and Eve are a couple who met on Christmas Eve. Bob is a regular guy."
r = /\b(?:[a-z]|([a-z])[a-z]*\1)\b/i
This regular expression reads, "match a word break, followed by one letter or by two or more letters with the last matching the first (case insensitive), all followed by a word break".
enum = str.each_match(r)
#=> #<Enumerator: "Viv and Bob are party...a regular guy.":gsub(/\b(?:[a-z]|([a-z])[a-z]*\1)\b/i)>
We can convert `enum` to an array to see the words that will be generated by the enumerator and passed to the block.
enum.to_a
#=> ["Viv", "Bob", "Bob", "Eve", "a", "Eve", "Bob", "a", "regular"]
Continuing,
enum.each_with_object(Hash.new(0)) { |word, h| h[word] += 1 }
#=> {"Viv"=>1, "Bob"=>3, "Eve"=>2, "a"=>2, "regular"=>1}
We could alternatively use `each_match` with a block.
h = Hash.new(0)
str.each_match(r) { |word| h[word] += 1 }
#=> "Viv and Bob are party animals. Bob and Eve are a couple who met on Christmas Eve. Bob is a regular guy."
h #=> {"Viv"=>1, "Bob"=>3, "Eve"=>2, "a"=>2, "regular"=>1}
This form of `each_match` has no counterpart with `gsub`.
Consider now how `scan` would be used here. Because of the way `scan` treats capture groups, we cannot write
str.scan(r)
#=> [["V"], ["B"], ["B"], ["E"], [nil], ["E"], ["B"], [nil], ["r"]]
Instead we must add a second capture group.
arr = str.scan(/\b((?:[a-z]|([a-z])[a-z]*\2))\b/i)
#=> [["Viv", "V"], ["Bob", "B"], ["Bob", "B"], ["Eve", "E"], ["a", nil], ["Eve", "E"], ["Bob", "B"], ["a", nil], ["regular", "r"]]
Then
arr.each_with_object(Hash.new(0)) { |(word,_),h| h[word] += 1 }
#=> {"Viv"=>1, "Bob"=>3, "Eve"=>2, "a"=>2, "regular"=>1}
This works but it's a bit of a [dog's breakfast](https://dictionary.cambridge.org/us/dictionary/english/a-dog-s-breakfast) when compared to the use of the proposed method.
The problem with using `gsub` in this way is that it is confusing to readers who are expecting character substitutions to be performed. I also believe that the name of this method (the "sub" in `gsub`) has resulted in the form of the method that returns an enumerator to be under-appreciated and under-used.
Some comments below propose that, in time, the form of `gsub` that returns an enumerator be deprecated.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>