From: "trans (Thomas Sawyer)" Date: 2012-07-02T02:59:29+09:00 Subject: [ruby-core:46077] [ruby-trunk - Feature #6670] str.chars.last should be possible Issue #6670 has been updated by trans (Thomas Sawyer). While I realize it doesn't exactly fit with the whole iteration thing particularly well, I nonetheless do not think it's unreasonable for Enumerator to support something like #last(n=1). In most cases that just means it has to iterate on down to the end and deliver the result. In some cases it might be able to optimize, say if the enumerable has a fixed size. This issue might also have some relation to the issue about Enumerable#size, #6636. ---------------------------------------- Feature #6670: str.chars.last should be possible https://bugs.ruby-lang.org/issues/6670#change-27684 Author: yhara (Yutaka HARA) Status: Assigned Priority: Normal Assignee: matz (Yukihiro Matsumoto) Category: core Target version: 2.0.0 =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 -- http://bugs.ruby-lang.org/