Feature #1880
Hash#flatten Doesn't Operate Recursively for Hash Values
| Status: | Assigned | Start date: | 08/05/2009 | |
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | % Done: | 0% |
||
| Category: | core | |||
| Target version: | 2.0.0 |
Description
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.
History
Updated by Marc-Andre Lafortune over 2 years ago
- Assignee set to Yukihiro Matsumoto
- Priority changed from Low to Normal
- Target version set to 1.9.2
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?
Updated by Yusuke Endoh almost 2 years ago
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 <mame@tsg.ne.jp>
Updated by Kazuhiro NISHIYAMA almost 2 years ago
- Target version changed from 1.9.2 to 2.0.0
Updated by Shyouhei Urabe over 1 year ago
- Status changed from Open to Assigned