Project

General

Profile

Actions

Feature #4778

closed

IO#each_chomped

Added by yimutang (Joey Zhou) over 13 years ago. Updated about 12 years ago.

Status:
Rejected
Target version:
-
[ruby-core:36463]

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

feature4778.pdf (56.3 KB) feature4778.pdf Eregon (Benoit Daloze), 07/01/2012 07:32 AM

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 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/4778

Author: 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

--
http://redmine.ruby-lang.org

Updated by Eregon (Benoit Daloze) over 12 years ago

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

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

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0