Project

General

Profile

Actions

Bug #1554

closed

Enumerator#first & #take should consume only what is needed [patch]

Added by marcandre (Marc-Andre Lafortune) over 15 years ago. Updated over 13 years ago.

Status:
Closed
Assignee:
-
Target version:
ruby -v:
ruby 1.9.2dev (2009-06-01 trunk 23616) [i386-darwin9.7.0]
Backport:
[ruby-core:23661]

Description

=begin
Currently, in Ruby 1.9.x:

require 'stringio'
s = StringIO.new("first \n second \n third")

s.rewind
p s.take(1), s.take(1)

Prints "[first \n]" and "[ second \n]"

s.rewind
p [s.first], [s.first]

Prints "[first \n]" and "[ second \n]"

s.rewind
p s.first(1), s.first(1)

Prints "[first \n]" and "[third]"

I believe most people would expect that [s.first], s.first(1) and s.take(1) should be the same and have the same side effects, if any. It is also more efficient that :each yields just the right number of times necessary to complete the request. As such it would be preferable if the last printout was the same as the previous two.

Note that in Ruby 1.8.7, the output for Enumerable#take is also wrong.

The included patches fix this issue for both versions.

I took the opportunity to change #first so that it calls :to_int on its argument only once if it needs to be converted. Before the patch it is called twice. This brings it in line with Array#first and Enumerable#take.

Note: rubyspecs have been updated.
=end


Files

enum_first_19.patch (709 Bytes) enum_first_19.patch marcandre (Marc-Andre Lafortune), 06/02/2009 05:38 AM
enum_first_187.patch (1.11 KB) enum_first_187.patch marcandre (Marc-Andre Lafortune), 06/02/2009 05:38 AM
Actions #1

Updated by matz (Yukihiro Matsumoto) over 15 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

=begin
Applied in changeset r23622.
=end

Actions

Also available in: Atom PDF

Like0
Like0