From: Dirk Traulsen Date: 2008-01-30T02:29:34+09:00 Subject: Re: Proposal/RFQ: Hash#values/keys with block Am 29 Jan 2008 um 22:50 hat Robert Klemme geschrieben: > I am waiting to see whether this is generally needed. As far as I > remember this has not been suggested before. If there is no > overwhelming request for this I opt for not making this standard. Why make it standard? 1. No regression Until now, a block after Hash#keys/values is just ignored. So, there would be no regression for old code. 2. No clutter As it is just an additional behaviour of exiting methods, there would be no new methods cluttering the standard library. 3. Speed I think that this will not slow down ruby as a whole, but I'm sure that hash.values {|k,v| v % 2 == 0 } would be definitely faster than hash.select {|k,v| v % 2 == 0 }.map {|k,v| v} especially with big hashes, because you make only one array directely, not two and don't have to call map additionally. And let us not begin about inject and speed.. 4. Rubyness This is difficult to describe, but one point I really, really like about Ruby is blocks. There are a lot of methods in the standard library which use blocks to 'finetune' the method. Use them pure and you get all of it. Or use them with a block and you get a subset or a modified result. I got so used to it, that I first wrote Hash#keys with a block without looking in ri before. I think this would fit perfectly in, as it is, at least for me, typical Ruby syntax. 5. Beauty I really like inject. (Though maybe not as much as you do, Robert. :) ) But if I compare hash.values {|k,v| k.size == 1 } with hash.select {|k,v| k.size == 1 }.map {|k,v| v} hash.inject([]) {|ks,(k,v)| ks << v if k.size == 1; ks} then I think it is obvious, why I really prefer the first variant. It is direct, short and clear. Less characters, but more obvious. That's what I love about Ruby ======= Robert, don't you think it would fit nicely in the standard lib? What do the others think? Please tell me your opinion and give your +1/-1 to the extension of Hash#key/values in the standard lib! Dirk