From: mail@... Date: 2018-05-23T10:38:29+00:00 Subject: [ruby-core:87238] [Ruby trunk Feature#14783] String#chars_at / String#bytes_at Issue #14783 has been updated by sos4nt (Stefan Sch����ler). Hanmac (Hans Mackowiak) wrote: > why does it return a new string instead of array of strings? Hanmac (Hans Mackowiak) wrote: > why does it return a new string instead of array of strings? Because `String#[]` also returns a string when given a range: ```ruby a = [1, 2, 3, 4] s = "1234" a[1..2] #=> [2, 3] s[1..2] #=> "23" ``` Accordingly: ```ruby a.values_at(1..2) #=> [2, 3] s.values_at(1..2) #=> "23" ``` ---------------------------------------- Feature #14783: String#chars_at / String#bytes_at https://bugs.ruby-lang.org/issues/14783#change-72226 * Author: sos4nt (Stefan Sch����ler) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- 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_str` * `bytes_at(selector, ...) ��� new_str` which work basically like [`Array#values_at`](http://ruby-doc.org/core/Array.html#method-i-values_at), e.g.: ```ruby string = 'hello, world!' string.chars_at(0, 5, 7, 12) #=> "h,w!" string.chars_at(0..4, 7..11) #=> "helloworld" ``` -- https://bugs.ruby-lang.org/ Unsubscribe: