[#105450] [Ruby master Feature#18228] Add a `timeout` option to `IO.copy_stream` — "byroot (Jean Boussier)" <noreply@...>
SXNzdWUgIzE4MjI4IGhhcyBiZWVuIHJlcG9ydGVkIGJ5IGJ5cm9vdCAoSmVhbiBCb3Vzc2llciku
11 messages
2021/09/27
[ruby-core:105299] [Ruby master Feature#18172] MatchData#sublen to return the length of a substring
From:
"Hanmac (Hans Mackowiak)" <noreply@...>
Date:
2021-09-16 13:36:12 UTC
List:
ruby-core #105299
Issue #18172 has been updated by Hanmac (Hans Mackowiak).
@nobu, isn't your `MatchData#match` the same as `MatchData#[]` ?
----------------------------------------
Feature #18172: MatchData#sublen to return the length of a substring
https://bugs.ruby-lang.org/issues/18172#change-93709
* Author: nobu (Nobuyoshi Nakada)
* Status: Closed
* Priority: Normal
----------------------------------------
There are many code taking the length of a substring matched by `Regexp`.
For instance, in rdoc/markup/attribute_manager.rb:
```ruby
attr_updated = attrs.set_attrs($`.length + $1.length + $2.length, $3.length, attr)
if attr_updated
$1 + NULL * $2.length + $3 + NULL * $2.length + $4
```
People often tends to use such code (although the first addition can be simpler as `$~.begin(3)`), that creates and soon drops substrings, just to take the length.
Therefore, how about the new method to calculate the length, `MatchData#sublen`?
```ruby
/(\d+)\W(\w+)/ =~ "1:foo"
$~.sublen(1) #=> 1
$~.sublen(2) #=> 3
```
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>