The 'flattenx' package on RAA does exactly this. I put it there two or three years ago, and I've never heard anyone mention having used it, which may mean that this is not a popular concept (or it may mean that the "Ruby doesn't have any libraries" cliche has once again prevailed and stopped people from realizing that there is a lot out there).
In any case, I tend to think it should be a different method name, like flatten_by(x), but I definitely like the idea.
-- David Black
In Ruby it's just a three-liner, which takes less time than starting a webbrowser. I think such features should be at least in the standard library, to become common practice. Beyond that, converting arrays efficiently into hashs is a core language aspect. While thinking about it, i realize, it is maybe better to have a pendant to Hash#to_a. There exist some similar symmetries in Ruby like 10.to_s.to_i . I do know only Hash[*list], but Array#to_h would make more sense with tuples, which makes the flatten call in the RCR example above obsolete.
-- Matthias Georgi
For the record, 'flattenx' is neither a three-liner nor in Ruby; it's in C, for speed. I don't agree in a general sense that everything that either is potentially useful or is a candidate for speed should be in the core language. But I still would like to see #flatten_by(n) or equivalent.
I'm not sure what you mean by 'a pendant to Hash#to_a' -- can you explain? #to_h has been under discussion/debate for a long time.... see ruby-talk archives (if you have time and patience :-)
-- David Black
I read the thread started by Gavin Sinclair, which lead to RCR #148. I don't like it, because you cannot insert keys from a block, so not flexible enough.
I understand the point of error handling. Maybe, to_h should treat non-two-element-arrays like follows: [ :a, [:b], [:c, :d], [:e, :g, :h] ].to_h => { :a => nil, :b => nil, :c => :d, :e => g } So each array element gives one key at least.
But that's maybe too confusing, so flatten_by() would be better.
Such little things as C-Extensions aren't very convenient, if you have many of them a ruby installation with a full set of libraries will get complicated and dependant, to put flattenx into ext/ is also not the best solution, because of code duplication.
-- Matthias Georgi
The 'flattenx' package on RAA does exactly this. I put it there two or three years ago, and I've never heard anyone mention having used it, which may mean that this is not a popular concept (or it may mean that the "Ruby doesn't have any libraries" cliche has once again prevailed and stopped people from realizing that there is a lot out there).
In any case, I tend to think it should be a different method name, like flatten_by(x), but I definitely like the idea.
-- David Black
In Ruby it's just a three-liner, which takes less time than starting a webbrowser. I think such features should be at least in the standard library, to become common practice. Beyond that, converting arrays efficiently into hashs is a core language aspect. While thinking about it, i realize, it is maybe better to have a pendant to Hash#to_a. There exist some similar symmetries in Ruby like 10.to_s.to_i . I do know only Hash[*list], but Array#to_h would make more sense with tuples, which makes the flatten call in the RCR example above obsolete.
-- Matthias Georgi
For the record, 'flattenx' is neither a three-liner nor in Ruby; it's in C, for speed. I don't agree in a general sense that everything that either is potentially useful or is a candidate for speed should be in the core language. But I still would like to see #flatten_by(n) or equivalent.
I'm not sure what you mean by 'a pendant to Hash#to_a' -- can you explain? #to_h has been under discussion/debate for a long time.... see ruby-talk archives (if you have time and patience :-)
-- David Black
I read the thread started by Gavin Sinclair, which lead to RCR #148. I don't like it, because you cannot insert keys from a block, so not flexible enough.
I understand the point of error handling. Maybe, to_h should treat non-two-element-arrays like follows: [ :a, [:b], [:c, :d], [:e, :g, :h] ].to_h => { :a => nil, :b => nil, :c => :d, :e => g } So each array element gives one key at least.
But that's maybe too confusing, so flatten_by() would be better.
Such little things as C-Extensions aren't very convenient, if you have many of them a ruby installation with a full set of libraries will get complicated and dependant, to put flattenx into ext/ is also not the best solution, because of code duplication.
-- Matthias Georgi