Feature #6532
closedMore methods for Hash and Array: Hash#has_all_keys? and family, Array#delete_at for multiple arguments
Description
I suggest introducing Hash methods: has_all_keys?, has_any_key?, has_none_key?, has_one_key?
to write: input.has_all_keys? %w{A C G T}
instead of: %w{A C G T}.all?{|letter| input.has_key? letter}
and to write args.has_one_key?(:input_file, :input_string, :input_from_another_source, ...) instead of obvious but verbose code with iterating through keys
Also I suggest reimplement method Array#delete_at to work with multiple keys:
def delete_at(*args)
args.sort.reverse{|index| current_version_of_delete_at index}
end
It helps not only writing multiple deletion code faster but also prevents mistakes with deleting elements from an array in arbitrary order
Updated by prijutme4ty (Ilya Vorontsov) almost 12 years ago
And of course I forgot the most useful method (more suggestions about names highly appreciate) Array#selection and Hash#selection
alphabet = %w{a b c d e f g h i j}
alphabet.selection(0,1,5,7) # ==> ['a','b','f','h']
{A: 1, T: 4, G: 3, C: 5}.selection(:A,:C,:G,:T) # => [1,5,3,4]
I have several unsolved issues:
1)what's the proper name for a method;
2)whether this method should return array or enumerator
3) whether it should work with block like #map:
{A: 1, T: 4, G: 3, C: 5}.selection(:A,:C,:G,:T){|el| el.to_f} # => [1.0, 5.0, 3.0, 4.0]
or like #each
{A: 1, T: 4, G: 3, C: 5}.selection(:A,:C,:G,:T){|el| print el} # => 1534
Updated by Eregon (Benoit Daloze) almost 12 years ago
You should have a look at #values_at about #selection.
I'm against has_*_keys?, because I think using any?,all?,... is clearer and not too verbose. (If it did look like C with an explicit loop, then I would consider it verbose).
About #delete_at with multiple arguments, I'm neutral, because this is inherently inefficient because all the elements after the index being deleted need to be moved, at every single deletion. A Set might be more appropriate in these circumstances.
Updated by marcandre (Marc-Andre Lafortune) almost 12 years ago
- Status changed from Open to Rejected
I've written a short wiki on making feature requests: http://bugs.ruby-lang.org/projects/ruby/wiki/HowToRequestFeatures
I'm inviting others to review and edit it...
I'm rejecting this feature request as it contains many different requests. Besides most of them appear to have been poorly thought out and researched. Please open new requests for those you still feel confident about, bearing in mind that:
- your
selection
is probably not useful givenvalues_at
- your ruby implementation of delete_at is probably not what you meant (unless you meant that arr.delete_at(0,0,0) would delete the first, second and third element and that you didn't think about the return value of
delete_at
, which makes it unreasonable to extend how you request it - you need to build a solid case for you has__keys? as there are many simple alternatives (e.g. using sets, (h.keys & %w[A C G T]).empty?, h.values_at(%w[A C G T]).any? ...)