Project

General

Profile

Feature #9179

MatchData#values_at should support named capture

Added by vzvu3k6k (vzvu3k6k _) almost 5 years ago. Updated over 2 years ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:58701]

Description

=begin
Using (({MatchData#[]})), we can access a named capture by its name.

/(?...)/.match("foo")[:a] #=> "foo"
/(?...)/.match("foo")["a"] #=> "foo"

But (({MatchData#values_at})) accepts only Integer and refuses Symbol or String.

/(?...)/.match("foo").values_at(:a)
# I expected it returns ["foo"], but...
#=> in `values_at': no implicit conversion of Symbol into Integer (TypeError)

/(?...)/.match("foo").values_at("a")
#=> in `values_at': no implicit conversion of String into Integer (TypeError)

(These are tested in ruby 2.1.0dev (2013-11-28 trunk 43903) [i686-linux].)

I think (({MatchData#values_at})) should work along with (({MathData#[]})) as follows:

/(?...)/.match("foo").values_at(:a) #=> ["foo"]
/(?...)/.match("foo").values_at("a") #=> ["foo"]

In the patch, (({match_entry})) is removed because it is no longer used. Comments and code are based on (({rb_hash_values_at})) in hash.c. It passes (({make test-all})) except (({TestGc#test_gc_reason})), which fails also on the original version.
=end

matchdata-values_at-named_capture.diff (1.89 KB) matchdata-values_at-named_capture.diff vzvu3k6k (vzvu3k6k _), 11/29/2013 11:23 PM

Associated revisions

Revision 92f8d74a
Added by naruse (Yui NARUSE) over 2 years ago

  • re.c (match_values_at): MatchData#values_at supports named captures
    [Feature #9179]

  • re.c (namev_to_backref_number): separeted.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55036 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 55036
Added by naruse (Yui NARUSE) over 2 years ago

  • re.c (match_values_at): MatchData#values_at supports named captures
    [Feature #9179]

  • re.c (namev_to_backref_number): separeted.

Revision 55036
Added by naruse (Yui NARUSE) over 2 years ago

  • re.c (match_values_at): MatchData#values_at supports named captures
    [Feature #9179]

  • re.c (namev_to_backref_number): separeted.

Revision 55036
Added by naruse (Yui NARUSE) over 2 years ago

  • re.c (match_values_at): MatchData#values_at supports named captures
    [Feature #9179]

  • re.c (namev_to_backref_number): separeted.

History

#1 [ruby-core:66095] Updated by naruse (Yui NARUSE) almost 4 years ago

  • Status changed from Open to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)

I want to write following code, how do you think matz?

reg = %r<\A(?<scheme>[A-Za-z][+\-.0-9A-Za-z]*):(?<hier-part>//(?<authority>[^/\\?#]+)(?<path-absolute>/[^?#]*)|[^/?#][^?#]*|)(?:\?(?<query>[^#]*))?(?:#(?<fragment>.*))?\z>
url = "http://example.com/hoge?foo=Bar#frag"
reg =~ url
p $~.values_at(:scheme, :authority, :"path-absolute", :query, :fragment)

#2 Updated by naruse (Yui NARUSE) over 2 years ago

  • Status changed from Assigned to Closed

Applied in changeset r55036.


  • re.c (match_values_at): MatchData#values_at supports named captures
    [Feature #9179]

  • re.c (namev_to_backref_number): separeted.

Also available in: Atom PDF