From: "yhara (Yutaka HARA)" Date: 2012-12-01T02:02:55+09:00 Subject: [ruby-core:50401] [ruby-trunk - Feature #6670] str.chars.last should be possible Issue #6670 has been updated by yhara (Yutaka HARA). duerst (Martin D��rst) wrote: > For with_index in particular, wouldn't it make sense to either add it to Enumerable or deprecate it on Enumerator? That would eliminate one more difference. If we add Enumerable#with_index, we could write str.with_index. This does not make sense becuase String#each is not defined. drbrain (Eric Hodel) wrote: > I often use StringIO as an IO substitute for tests. I would prefer matching behavior. > > You can get the last line by stringio.string.lines.last. Is this acceptable? Yes. And I got a reply from @knu: https://twitter.com/knu/status/274442544518156288 He said "We should not fix API of StringIO before fixing that of IO" and I agreed. ---------------------------------------- Feature #6670: str.chars.last should be possible https://bugs.ruby-lang.org/issues/6670#change-34228 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/