Feature #18172
closedMatchData#sublen to return the length of a substring
Description
There are many code taking the length of a substring matched by Regexp.
For instance, in rdoc/markup/attribute_manager.rb:
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?
Updated by matz (Yukihiro Matsumoto) almost 5 years ago
In Ruby C source code, we use names like sublen but Ruby methods tend to be fully spelled. So I propose MatchData#match(n) and MatchData#match_length(n).
Matz.
Updated by nobu (Nobuyoshi Nakada) almost 5 years ago
- Status changed from Open to Closed
Applied in changeset git|09d724e6f846b4e53e8571d41ca7d3055d732d9f.
[Feature #18172] Add MatchData#match
The method to return the single matched substring corresponding to
the given argument.
Updated by Hanmac (Hans Mackowiak) almost 5 years ago
@nobu (Nobuyoshi Nakada), isn't your MatchData#match the same as MatchData#[] ?
Updated by nobu (Nobuyoshi Nakada) almost 5 years ago
Hanmac (Hans Mackowiak) wrote in #note-3:
@nobu (Nobuyoshi Nakada), isn't your
MatchData#matchthe same asMatchData#[]?
Similar, but #match accepts only single index/name, but not a range or an optional length.
Updated by Hanmac (Hans Mackowiak) almost 5 years ago
nobu (Nobuyoshi Nakada) wrote in #note-4:
Hanmac (Hans Mackowiak) wrote in #note-3:
@nobu (Nobuyoshi Nakada), isn't your
MatchData#matchthe same asMatchData#[]?Similar, but
#matchaccepts only single index/name, but not a range or an optional length.
i just wonder why not use the functions there like done in the other method?
Updated by nobu (Nobuyoshi Nakada) almost 5 years ago
match_ary_aref may return an Array of substrings when idx is a Range.