ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "andrykonchin (Andrew Konchin)" <noreply@ruby-lang.org>
To: ruby-core@neon.ruby-lang.org
Subject: [ruby-core:110767] [Ruby master Misc#19131] MatchData#values_at(): addressing with Range and Integer index behaves differently for the first element (entire matched string)
Date: Tue, 15 Nov 2022 15:33:18 +0000 (UTC)	[thread overview]
Message-ID: <redmine.issue-19131.20221115153318.13553@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-19131.20221115153318.13553@ruby-lang.org

Issue #19131 has been reported by andrykonchin (Andrew Konchin).

----------------------------------------
Misc #19131: MatchData#values_at(): addressing with Range and Integer index behaves differently for the first element (entire matched string)
https://bugs.ruby-lang.org/issues/19131

* Author: andrykonchin (Andrew Konchin)
* Status: Open
* Priority: Normal
----------------------------------------
I've noticed a strange nuance and not sure whether it's intentional or not. In case it's intentional - it seems to me inconsistent.

I will illustrate it with the following example:

```ruby
/(.)(.)(.)(.)/.match("abcde").values_at(-5..-1) # => ["abcd", "a", "b", "c", "d"]
/(.)(.)(.)(.)/.match("abcde").values_at(-5) # => [nil]
```

So with a negative index we can address the whole matched string (`0` element) with a Range argument but cannot with an Integer index.

We can index with negative values only captured values:

```ruby
/(.)(.)(.)(.)/.match("abcde").values_at(-4) # => ["a"]
/(.)(.)(.)(.)/.match("abcde").values_at(-3) # => ["b"]
/(.)(.)(.)(.)/.match("abcde").values_at(-2) # => ["c"]
# ...
```

---

```
ruby -v
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-darwin21]
```



-- 
https://bugs.ruby-lang.org/

           reply	other threads:[~2022-11-15 15:33 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <redmine.issue-19131.20221115153318.13553@ruby-lang.org>]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.issue-19131.20221115153318.13553@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    --cc=ruby-core@neon.ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).