Project

General

Profile

Feature #2172

Enumerable#chunk with no block

Added by marcandre (Marc-Andre Lafortune) almost 8 years ago. Updated 12 months ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:25912]

Description

=begin
What should "(1..3).chunk" (i.e. without a block) do?

It issued an
ArgumentError: tried to create Proc object without a block

I changed the error message to "no block given" which I hope to be more informative, but maybe there is something more useful to do here?

A default block of {|x| x} doesn't seem all that useful.

Returning an enumerator that, upon completion, will return an enumerator would probably be better, but could also be a bit confusing if someone doesn't realize he forgot to specify the block?

Thanks to Run Paint for raising the question when writing the rubyspec for #chunk.
=end

Associated revisions

Revision 56342
Added by marcandre (Marc-Andre Lafortune) 12 months ago

  • enum.c: Make Enumerable#chunk with no block return an Enumerator [#2172]

Revision 56342
Added by marcandre (Marc-Andre Lafortune) 12 months ago

  • enum.c: Make Enumerable#chunk with no block return an Enumerator [#2172]

History

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

  • Status changed from Open to Assigned
  • Assignee changed from matz (Yukihiro Matsumoto) to akr (Akira Tanaka)

#2 Updated by marcandre (Marc-Andre Lafortune) almost 8 years ago

  • Category set to core
  • Assignee changed from akr (Akira Tanaka) to matz (Yukihiro Matsumoto)
  • Target version set to 1.9.2

#3 Updated by marcandre (Marc-Andre Lafortune) almost 8 years ago

  • Assignee changed from matz (Yukihiro Matsumoto) to akr (Akira Tanaka)

#5 Updated by znz (Kazuhiro NISHIYAMA) over 7 years ago

  • Target version changed from 1.9.2 to 2.0.0

#6 [ruby-core:47020] Updated by tokland (Arnau Sanchez) about 5 years ago

Any decision about this? For me it's a clear +1, chunking by the values unchanged is very common (not that it's representative, but I checked my code: 4 ocurrences of chunk, all with the identity block)

#7 [ruby-core:48520] Updated by akr (Akira Tanaka) almost 5 years ago

  • Description updated (diff)
  • Target version changed from 2.0.0 to next minor

#8 [ruby-core:77336] Updated by marcandre (Marc-Andre Lafortune) about 1 year ago

  • Assignee changed from akr (Akira Tanaka) to matz (Yukihiro Matsumoto)

Matz, would you agree to return enumerator for chunk without a block?
This is useful in particular to chain it with with_index

A real case example is to summarize a list of integers into ranges. Currently, we need:

integers.enum_for(:chunk).with_index { |x, idx| x - idx }.map do |diff, group|
  [group.first, group.last]
end

Ref: http://stackoverflow.com/questions/8621733/how-do-i-summarize-array-of-integers-as-an-array-of-ranges

#9 [ruby-core:77337] Updated by marcandre (Marc-Andre Lafortune) about 1 year ago

Forgot to note that the other possiblity suggested for chunk with no block can now be done clearly and succintly with chunk(&:itself), so there's no longer any doubt that the block without block should return an Enumerator

#10 [ruby-core:77481] Updated by matz (Yukihiro Matsumoto) 12 months ago

Now it sounds reasonable. Marc-Andre, could you implement it?

Matz.

#11 [ruby-core:77483] Updated by marcandre (Marc-Andre Lafortune) 12 months ago

Great, thanks!

Done for chunk.

Matz, I imagine I should do the same for chunk_while, slice_after, slice_before and slice_when, right?

#12 [ruby-core:77500] Updated by marcandre (Marc-Andre Lafortune) 12 months ago

  • Status changed from Assigned to Closed

Closing, created #12813 instead.

Also available in: Atom PDF