Feature #14783
openString#chars_at / String#bytes_at
Description
I just wanted to extract characters at certain indices from a string and noticed that there's no values_at counterpart for String.
I'd therefore like to propose two new String methods:
chars_at(selector, ...) → new_strbytes_at(selector, ...) → new_str
which work basically like Array#values_at, e.g.:
Updated by Hanmac (Hans Mackowiak) over 8 years ago
why does it return a new string instead of array of strings?
you might like this:
Updated by zverok (Victor Shepelev) over 8 years ago
Updated by sos4nt (Stefan Schüßler) over 8 years ago
Updated by nobu (Nobuyoshi Nakada) over 8 years ago
sos4nt (Stefan Schüßler) wrote:
Because
String#[]also returns a string in such case:
It's a different, single argument case.
I don't think that values_at should return same class as the receiver, as Hash#values_at.
Updated by shevegen (Robert A. Heiler) over 8 years ago
Is the frequency of #bytes_at common? I understand the use case
stated by Stefan (extract multiple indices via one method call
on a String) but #bytes_at may seem to be quite rare.
As for returning a String or Array, I personally expect #chars
on a String object to return an Array, so intuitively I would
assume #chars_at to also return an Array.