Feature #4778
closedIO#each_chomped
Description
When manipulating text files, mostly, the input record separator(saying "\n") is scrap and will be chomped.
I suggest a method IO#each_chomped directly yielding the record with input record separator chomped.
Laziness is a virtue..
Joey
Files
Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 13 years ago
I wouldn't say this is about laziness, but about code readability. I agree this is a common use, but doesn't each_line already do that?
Updated by yimutang (Joey Zhou) over 13 years ago
Rodrigo Rosenfeld Rosas wrote:
I wouldn't say this is about laziness, but about code readability. I agree this is a common use, but doesn't each_line already do that?
No, #each_line is just a synonym of #each.
Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 13 years ago
Yes, I was confused by the fact that puts already chomps the argument...
Updated by naruse (Yui NARUSE) over 13 years ago
- Status changed from Open to Assigned
- Assignee set to matz (Yukihiro Matsumoto)
Updated by headius (Charles Nutter) over 13 years ago
Trivial Ruby impl...
class IO
def each_chomped
each {|x| yield x.chomp!}
end
end
Of course if you don't mind the whole file being read into memory:
io.each.map(&:chomp).each { ...
Might be a way to pipeline that without reading into memory.
Updated by josb (Jos Backus) over 13 years ago
Charles, shouldn't
x.chomp!
be
x.chomp
in the above code? x.chomp! can return nil.
Updated by headius (Charles Nutter) over 13 years ago
Yeah, probably...or it should be x.chomp!; yield x
My intent was to avoid creating a useless transient object.
- Charlie (mobile)
On Jun 8, 2011, at 10:54, Jos Backus jos@catnook.com wrote:
Issue #4778 has been updated by Jos Backus.
Charles, shouldn't
x.chomp!
be
x.chomp
in the above code? x.chomp! can return nil.¶
Feature #4778: IO#each_chomped
http://redmine.ruby-lang.org/issues/4778Author: Joey Zhou
Status: Assigned
Priority: Normal
Assignee: Yukihiro Matsumoto
Category:
Target version:When manipulating text files, mostly, the input record separator(saying "\n") is scrap and will be chomped.
I suggest a method IO#each_chomped directly yielding the record with input record separator chomped.
Laziness is a virtue..
Joey
Updated by Eregon (Benoit Daloze) over 12 years ago
- File feature4778.pdf feature4778.pdf added
Attached one-minute slide for this proposal.
I think the name might still be matter of discussion (it could maybe even be an option of #each_line), but it would be interesting to get the general idea evaluated.
Updated by mame (Yusuke Endoh) over 12 years ago
Received, thank you!
I missed (or forgot) this feature request, but I love it.
Thank you putting this in the spotlight!
--
Yusuke Endoh mame@tsg.ne.jp
Updated by trans (Thomas Sawyer) over 12 years ago
How about just each_chomp(str="\n")
?
Updated by mame (Yusuke Endoh) over 12 years ago
- Status changed from Assigned to Rejected
Benoit Daloze,
Sorry but this proposal was rejected at the developer meeting (7/21).
Both feature itself and name are not so bad.
But the reason it was rejected is fear of endless similar proposals.
So, please start it as a gem.
Matz might reconsider it if your gem really becomes widely used.
--
Yusuke Endoh mame@tsg.ne.jp