Project

General

Profile

Actions

Feature #5766

closed

Hash.each_with_object should behave differently when block's arity is 3

Added by rosenfeld (Rodrigo Rosenfeld Rosas) over 12 years ago. Updated over 12 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:41668]

Description

Enum.each_with_object is very handy in lots of situations, but specially while dealing with arrays. It would be great to be able to use it with hashes like below:

{a: 1}.each_with_object([]){|k, v, object| ...}

Currently, we need to do

{a: 1}.each_with_object([]) do |pair, object|
k, v = pair
...
end

Updated by matz (Yukihiro Matsumoto) over 12 years ago

Hi,

In message "Re: [ruby-core:41668] [ruby-trunk - Feature #5766][Open] Hash.each_with_object should behave differently when block's arity is 3"
on Thu, 15 Dec 2011 13:07:22 +0900, Rodrigo Rosenfeld Rosas writes:

|Enum.each_with_object is very handy in lots of situations, but specially while dealing with arrays. It would be great to be able to use it with hashes like below:
|
|{a: 1}.each_with_object([]){|k, v, object| ...}

Try

{a: 1}.each_with_object([]){|(k, v), object| ...}

Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 12 years ago

How do I close this issue?

Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 12 years ago

Em 15-12-2011 03:14, Yukihiro Matsumoto escreveu:

Hi,

In message "Re: [ruby-core:41668] [ruby-trunk - Feature #5766][Open] Hash.each_with_object should behave differently when block's arity is 3"
on Thu, 15 Dec 2011 13:07:22 +0900, Rodrigo Rosenfeld Rosas writes:

|Enum.each_with_object is very handy in lots of situations, but specially while dealing with arrays. It would be great to be able to use it with hashes like below:
|
|{a: 1}.each_with_object([]){|k, v, object| ...}

Try

{a: 1}.each_with_object([]){|(k, v), object| ...}

Great, thanks Matz! I didn't know that. I had tried {|[k, v], object|}
before but haven't think about the parenthesis :)

Updated by shyouhei (Shyouhei Urabe) over 12 years ago

  • Status changed from Open to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0