Project

General

Profile

Actions

Feature #8396

closed

Allow easier destructuring of MatchData

Added by goshakkk (Gosha Arinich) almost 11 years ago. Updated almost 11 years ago.

Status:
Feedback
Assignee:
-
Target version:
-
[ruby-core:54929]

Description

=begin

(((I have opened ((<"a PR for this on GH @ rails/rails"|URL:https://github.com/ruby/ruby/pull/300>)) for this but @marcandre (Marc-Andre Lafortune) closed it and suggested I submit it here. I'll copy my proposal text here.)))

Currently, you can get something out of (({MatchData})) like this:

m = "2_apples".match /(\d+)_(apple|orange)s?/
count, thing = m[1], m[2]

or

, count, thing = "2_apples".match(/(\d+)(apple|orange)s?/).to_a

or

count, thing = "2_apples".match(/(\d+)_(apple|orange)s?/).captures

or, as @marcandre (Marc-Andre Lafortune) suggested, you can use splat operator

, count, thing = * "2_apples".match /(\d+)(apple|orange)s?/

However, this extra (({#to_a})) or (({#captures})) (or splat) that you have to add makes things slightly more verbose.

With this PR, it would be possible to do the following:

count, thing = "2_apples".match /(\d+)_(apple|orange)s?/

Which looks a bit cleaner and nicer. So what do you think?

=end


Files

match_data_to_ary.patch (827 Bytes) match_data_to_ary.patch goshakkk (Gosha Arinich), 05/12/2013 06:49 PM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0