From: "yhara (Yutaka HARA)" Date: 2012-11-28T01:32:55+09:00 Subject: [ruby-core:50231] [ruby-trunk - Feature #6670] str.chars.last should be possible Issue #6670 has been updated by yhara (Yutaka HARA). trans (Thomas Sawyer) wrote: > If I understand correctly, this is going to break a lot of code? For String, the impact will be limited. * String#lines returns Array, which has most of the methods defined in Enumerator. * Exceptions are #next, #peek, #with_index, etc. If you have a code like `str.lines.with_index', you need to change it to `str.each_line.with_index'. * When you have a huge string, `str.lines' will become slower and consume more memory. I think this is a rare case because we usually avoid `File.read(path_to_huge_file)'. Even when you really need huge_str.lines to return Enumerator, you can use huge_str.each_line instead. For IO/StringIO/ARGF/GzipReader, I'd like to +1 for showing deprecation warning in 2.0.0. ---------------------------------------- Feature #6670: str.chars.last should be possible https://bugs.ruby-lang.org/issues/6670#change-34050 Author: yhara (Yutaka HARA) Status: Assigned Priority: Normal Assignee: yhara (Yutaka HARA) 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/