Project

General

Profile

Actions

Feature #7654

open

Add optional code block to IO::readlines

Feature #7654: Add optional code block to IO::readlines

Added by shock_one (Володимир Шацький) almost 13 years ago. Updated over 1 year ago.

Status:
Assigned
Target version:
-
[ruby-core:51240]

Description

Of course, we always can write something like
File.readlines('/home/shock_one/test.rb').map{ |line| line.upcase }
but this way we create unneeded intermediate array which can be pretty big.
There is also a method IO::foreach, but it doesn't collect return values.
Besides it seems pretty logical and natural to have a block in this method.

Updated by shock_one (Володимир Шацький) almost 13 years ago Actions #1 [ruby-core:51241]

Just to be clear: code block will allow to write the first code snippet as follows:
data = File.readlines('/home/shock_one/test.rb'){ |line| line.upcase }

Updated by Eregon (Benoit Daloze) almost 13 years ago Actions #2 [ruby-core:51244]

Why not

File.foreach('test.rb').map { |line| line.upcase }

?
It does not create an intermediary Array.
If you need to do other operations lazily (without an intermediate result), you could use #lazy:

File.foreach('test.rb').lazy.select { |line| line.start_with? '/' }.map { |line| line.upcase }.to_a

Updated by Eregon (Benoit Daloze) almost 13 years ago Actions #3 [ruby-core:51245]

But of course the main memory usage here are likely String instances, so you could update them in place if possible:

File.foreach('test.rb').map { |line| line.upcase!; line }
# or
lines = File.readlines('test.rb')
lines.each(&:upcase!)

Updated by Eregon (Benoit Daloze) almost 13 years ago Actions #4 [ruby-core:51246]

(There is also Array#map!)

Updated by shock_one (Володимир Шацький) almost 13 years ago Actions #5 [ruby-core:51247]

Thank you, Eregon, especially for in place methods. I should definitely pay more attention to them - functional languages made me a little suspicious of this sort of things.
But I still think it would be nice to have an optional block in IO::readlines, at least for convenience. Enumerable#grep has similar behavior and I find it really cool and useful. Also this change doesn't break anything, is easy to implement and makes the code more concise.

Updated by ko1 (Koichi Sasada) over 12 years ago Actions #6 [ruby-core:51643]

next minor issue or 2.0.0 issue?

Updated by drbrain (Eric Hodel) over 12 years ago Actions #7 [ruby-core:51671]

I think next minor. It is a new feature but trunk is closed for new features.

Updated by drbrain (Eric Hodel) over 12 years ago Actions #8 [ruby-core:51676]

  • Target version set to 2.6

Updated by ko1 (Koichi Sasada) over 12 years ago Actions #9 [ruby-core:52656]

  • Assignee set to matz (Yukihiro Matsumoto)

Updated by naruse (Yui NARUSE) almost 8 years ago Actions #10

  • Target version deleted (2.6)

Updated by hsbt (Hiroshi SHIBATA) over 1 year ago Actions #11

  • Status changed from Open to Assigned
Actions

Also available in: PDF Atom