Project

General

Profile

Actions

Feature #18172

closed

MatchData#sublen to return the length of a substring

Added by nobu (Nobuyoshi Nakada) about 3 years ago. Updated about 3 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:105290]

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?

/(\d+)\W(\w+)/ =~ "1:foo"
$~.sublen(1)    #=> 1
$~.sublen(2)    #=> 3
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0