From: shevegen@... Date: 2018-04-18T16:54:11+00:00 Subject: [ruby-core:86584] [Ruby trunk Feature#6670] str.chars.last should be possible Issue #6670 has been updated by shevegen (Robert A. Heiler). While I myself have not needed str.chars.last, I can understand why Yutaka HARA suggested it. Makes sense to me personally- One comment on this: > I feel that the lack of #last is a good thing. It's there > to remind you that it might be expensive, and that (for > example) it would be worth storing in a local var instead > of calling it twice. In general, in ruby, speed considerations are not the first typical thought that may come to every ruby hacker. And there is a big difference between (a) something that is slow, but possible, and (b) something that may be slow, but is not possible. I think in this case, (a) applies and the speed consideration comes (or should come) secondary - so I don't feel that using speed as an example, should be something that speaks against the proposal itself. IMO the question should be whether this is sufficiently useful or not. I can't answer this either, but from that point of view, I think the proposal by Yutaka HARA makes sense. Matz will probably say something about the suggestion in the upcoming developer meeting. :) PS: The documentation can also mention whether there are speed considerations, so I think this is another reason why speed considerations should not come primary as opposed to functionality that may be useful (whether it is, I do not know; just mentioning this in general). ---------------------------------------- Feature #6670: str.chars.last should be possible https://bugs.ruby-lang.org/issues/6670#change-71527 * Author: yhara (Yutaka HARA) * Status: Assigned * Priority: Normal * Assignee: matz (Yukihiro Matsumoto) * Target version: Next Major ---------------------------------------- =begin Since str.chars returns an Enumerator, we need explicit to_a for some operations: str.chars.to_a.last str.chars.to_a[1,3] But often I forget that and write: str.chars.last str.chars[1,3] Besides that, I feel it is hard to explain why to_a is needed here when I'm writing artilcles for Ruby beginners. Simplest way to achieve this is to make String#chars (also #lines, #bytes and #codepoints) return an Array. Since arrays have most of the methods defined in Enumerator, this will not be a big change. For programs like str.chars.next, you can use each_char instead. =end ---Files-------------------------------- 6670.pdf (39.4 KB) string_bytes_to_array.patch (27.4 KB) 0001-Deprecate-lines-bytes-chars-codepoints-of-IO-likes.patch (21.9 KB) -- https://bugs.ruby-lang.org/ Unsubscribe: