Project

General

Profile

Actions

Feature #1880

closed

Hash#flatten Doesn't Operate Recursively for Hash Values

Added by runpaint (Run Paint Run Run) over 14 years ago. Updated about 12 years ago.

Status:
Rejected
Target version:
[ruby-core:24745]

Description

=begin
Hash#flatten claims to flatten recursively if given an argument specifying the depth. This works as expected for Array values, but has no bearing on Hash values.

h = {orange: :orange, coconut: {inside: :white, outside: :brown}, grapes: {colour: [:red, :green]}}
h.flatten
=> [:orange, :orange, :coconut, {:inside=>:white, :outside=>:brown}, :grapes, {:colour=>[:red, :green]}]
h.flatten(20)
=> [:orange, :orange, :coconut, {:inside=>:white, :outside=>:brown}, :grapes, {:colour=>[:red, :green]}]

This seems peculiar. If the user has specified a depth he's explicitly stating the level of recursion to flatten; not how he wants Array values to be treated. IOW, I expected #flatten(depth) to call #flatten on Hash values as appropriate. This can be generalised to: call #flatten on values until the depth has been reached.
=end

Actions #1

Updated by marcandre (Marc-Andre Lafortune) over 14 years ago

  • Assignee set to matz (Yukihiro Matsumoto)
  • Priority changed from 3 to Normal
  • Target version set to 1.9.2

=begin
Moving this from 'bug' to 'feature request' as behavior corresponds to doc. Currently it converts to an array and (if given an argument) calls flatten on it. Array#flatten leaves hashes alone.

The question remaining: should there be a way to recursively flatten hashes, via Hash#flatten or otherwise?
=end

Actions #2

Updated by mame (Yusuke Endoh) about 14 years ago

=begin
Hi,

Hash#flatten claims to flatten recursively if given an argument specifying the depth. This works as expected for Array values, but has no bearing on Hash values.

I wonder why recursive Hash#flatten is needed :-)
I can't imagine any use case.

--
Yusuke Endoh
=end

Actions #3

Updated by znz (Kazuhiro NISHIYAMA) about 14 years ago

  • Target version changed from 1.9.2 to 2.0.0

=begin

=end

Actions #4

Updated by shyouhei (Shyouhei Urabe) over 13 years ago

  • Status changed from Open to Assigned

=begin

=end

Updated by mame (Yusuke Endoh) about 12 years ago

  • Status changed from Assigned to Rejected

I'm rejecting this feature ticket because no progress has been
made for a long time. See [ruby-core:42391].

I think flatten means an operation on Array, in Ruby.
"flatten" on Hash is also possible, but Hash#flatten is not for
that. I think we need:

  • a use case
  • another appropriate name
  • detailed semantics (what does {a: {a: 1}}.flatten return?)

--
Yusuke Endoh

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0