Project

General

Profile

Actions

Feature #13211

closed

Hash#delete taking a splat

Added by kddnewton (Kevin Newton) about 7 years ago. Updated 7 months ago.

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

Description

Hi there,

I would like to add the ability for Hash#delete to take a splat of arguments that would delete entries at each of the given keys. I've run into this quite a few times where it would be useful in application code. Something like

def preprocess(args = {})
  do_some_preprocessing(%i[a b c].zip(args.delete(:a, :b, :c)).to_h)
  process(args)
end

Would be very happy to work on a patch for this if it's desired.


Files

hash-delete-multiple.patch (3.35 KB) hash-delete-multiple.patch kddnewton (Kevin Newton), 04/29/2017 08:40 PM

Updated by sawa (Tsuyoshi Sawada) about 7 years ago

Whether to accept a splat (perhaps on an array) at the beginning of an argument is not something to be specified as a method signature.

Your real issue is to let the method accept an arbitrary number of arguments.

Whether you pass the method an array with a splat or you directly pass multiple arguments is solely up to how you use the method in the particular context.

Updated by kddnewton (Kevin Newton) about 7 years ago

Okay, yes, an arbitrary number of arguments. Is this something to which people would be amenable?

Updated by shyouhei (Shyouhei Urabe) almost 7 years ago

We looked at this issue in yesterday's developer meeting.

While extending Hash#delete is still a valid feature request, it seems to us that the OP wants something different; the example code shows that what is actually wanted is to split a hash into two. That is doable with Hash#delete, but not that intuitive or declarative.

You might want to request such feature instead.

Updated by kddnewton (Kevin Newton) almost 7 years ago

I've attached a patch of what I'm talking about, which should hopefully be more clear. The idea is that you could delete multiple values from a hash at once, which is valuable in its own right. Additionally, the return value is changed to be an array if multiple arguments are passed, representing the deleted values.

This is my first time contributing and I'm not sure if I'm doing everything right, so please any feedback is welcome.

Actions #5

Updated by kddnewton (Kevin Newton) 7 months ago

  • Status changed from Open to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0