Project

General

Profile

Feature #7006

Add Enumerable#grouped_in as a each_slice without the block

Added by Rodrigo Rosenfeld Rosas about 4 years ago. Updated about 4 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
[ruby-core:47474]

Description

I'd like to transform [1, 2, 3, 4] into [[1, 2], [3, 4]].

Currently I can use something like:

a = []; [1, 2, 3, 4].each_slice(2){|chunk| a << chunk}

But I'd prefer to write just:

a = [1, 2, 3, 4].grouped_in(2)

Or any other method name you would choose, like "slices" or something else.

History

#1 [ruby-core:47475] Updated by Jeremy Evans about 4 years ago

This should work:

a = [1, 2, 3, 4].each_slice(2).to_a

I don't see a reason to create another method to do this.

#2 [ruby-core:47478] Updated by Marc-Andre Lafortune about 4 years ago

  • Status changed from Open to Rejected

Indeed, to_a is the answer. Note that ActiveSupport has a similar in_groups_of that will return the results even when no block is given.

#3 [ruby-core:47485] Updated by Rodrigo Rosenfeld Rosas about 4 years ago

Thanks, I didn't know that. I can't close this issue myself. Could someone do that, please?

#4 [ruby-core:47487] Updated by Marc-Andre Lafortune about 4 years ago

rosenfeld (Rodrigo Rosenfeld Rosas) wrote:

Could someone do that, please?

Already done.

#5 [ruby-core:47499] Updated by Rodrigo Rosenfeld Rosas about 4 years ago

Ah, okay, thank you, didn't notice before :P

Also available in: Atom PDF