[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Suggestion: Hash.remove

Andrew Walrond

4/23/2005 10:18:00 AM

Something I often find useful is a version of Hash.delete which returns the
hash rather than the deleted item. Useful for terse chained code.

class Hash
def remove!(*some_keys)
some_keys.each { |key| delete(key) }
return self
end
def remove(*some_keys)
return dup.remove!(*some_keys)
end
end


Perhaps something like it is already planned for the next version? If not,
perhaps the Hash maintainer might consider something like it.

Andrew Walrond


13 Answers

Lyndon Samson

4/23/2005 11:35:00 AM

0

On 4/23/05, Andrew Walrond <andrew@walrond.org> wrote:
> Something I often find useful is a version of Hash.delete which returns the
> hash rather than the deleted item. Useful for terse chained code.
>
> class Hash
> def remove!(*some_keys)
> some_keys.each { |key| delete(key) }
> return self
> end
> def remove(*some_keys)
> return dup.remove!(*some_keys)
> end
> end
>
> Perhaps something like it is already planned for the next version? If not,
> perhaps the Hash maintainer might consider something like it.
>

I think there is a bias against method chaining by the Ruby Powers-that-Be.


> Andrew Walrond
>
>


--
Into RFID? www.rfidnewsupdate.com Simple, fast, news.



dblack

4/23/2005 11:41:00 AM

0

Florian Groß

4/23/2005 2:38:00 PM

0

Andrew Walrond wrote:

> Something I often find useful is a version of Hash.delete which returns the
> hash rather than the deleted item. Useful for terse chained code.

You want to do hash.delete("foo").delete("bar").delete("qux").

Why don't you do this?

%w(foo bar qux).each do |key|
hash.delete(key)
end

New keys can easily be added and is not repetitive at all.

Perhaps you could instead change your proposal to make .delete() accept
multiple keys?



dblack

4/23/2005 3:31:00 PM

0

Andrew Walrond

4/23/2005 5:04:00 PM

0

On Saturday 23 April 2005 15:37, Florian Groß wrote:
> Andrew Walrond wrote:
> > Something I often find useful is a version of Hash.delete which returns
> > the hash rather than the deleted item. Useful for terse chained code.
>
> You want to do hash.delete("foo").delete("bar").delete("qux").

Yes, but more usually just an assignment:

attribs = {'version'=>version}.update(@versions[version]).remove('files')

>
> Why don't you do this?
>
> %w(foo bar qux).each do |key|
> hash.delete(key)
> end
>

Because small code is often beautiful code, and I never use three lines to do
a one line job ;)

> New keys can easily be added and is not repetitive at all.
>
> Perhaps you could instead change your proposal to make .delete() accept
> multiple keys?

That would definately be a good, backwardly compatible setp.

Andrew



nobu.nokada

4/24/2005 12:45:00 AM

0

Hi,

At Sun, 24 Apr 2005 02:04:00 +0900,
Andrew Walrond wrote in [ruby-talk:139530]:
> > New keys can easily be added and is not repetitive at all.
> >
> > Perhaps you could instead change your proposal to make .delete() accept
> > multiple keys?
>
> That would definately be a good, backwardly compatible setp.

What will be returned?

--
Nobu Nakada


Florian Groß

4/24/2005 2:05:00 AM

0

nobu.nokada@softhome.net wrote:

>>>Perhaps you could instead change your proposal to make .delete() accept
>>>multiple keys?
>>
>>That would definately be a good, backwardly compatible setp.
>
> What will be returned?

An Array of the deleted values?



nobu.nokada

4/24/2005 2:49:00 AM

0

Hi,

At Sun, 24 Apr 2005 11:05:27 +0900,
Florian Groß wrote in [ruby-talk:139571]:
> >>>Perhaps you could instead change your proposal to make .delete() accept
> >>>multiple keys?
> >>
> >>That would definately be a good, backwardly compatible setp.
> >
> > What will be returned?
>
> An Array of the deleted values?

It's not compatible obviously. Another method Hash#remove (and
Array#remove also?) sounds better, I guess.

--
Nobu Nakada



Andrew Walrond

4/24/2005 5:43:00 AM

0

On Sunday 24 April 2005 03:49, nobu.nokada@softhome.net wrote:
> > >>
> > >>That would definately be a good, backwardly compatible setp.
> > >
> > > What will be returned?
> >
> > An Array of the deleted values?
>
> It's not compatible obviously. Another method Hash#remove (and
> Array#remove also?) sounds better, I guess.

delete could return a value, or array of values, depending on whether the
number of arguments is 1 or >1, which would be compatible.

But #remove would be nicer :)

Andrew Walrond


Florian Groß

4/24/2005 7:45:00 PM

0

nobu.nokada@softhome.net wrote:

>>>>>Perhaps you could instead change your proposal to make .delete() accept
>>>>>multiple keys?
>>>
>>>What will be returned?
>>
>>An Array of the deleted values?
>
> It's not compatible obviously. Another method Hash#remove (and
> Array#remove also?) sounds better, I guess.

Oh, I don't think so. There is other methods which also return an Array
or one value depending on their arguments. Array#last comes to mind.

It might still be better to have another method for this, though. I'm
not sure.