# Hash#slice

**URL:** <https://www.ruby-forum.com/t/hash-slice/134611>\
**Category:** Ruby\
**Created:** [April 12, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/hash-slice/134611 "2008-04-12T23:01:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ara\_howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ara\_howard](https://www.ruby-forum.com/u/ara_howard)\
**Post date:** [April 12, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/hash-slice/134611/1 "2008-04-12T23:01:25Z")

</div>

is this consistent with other peoples stdlib hacks?

class Hash  
def slice \*keys, &block  
if block  
each do |key, val|  
boolean = block.call(key, val)  
keys \<\< key if boolean  
end  
end  
hash = self  
keys.inject({}){|returned, key| returned.update key =\> hash[key]}  
end  
end

??

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![ara\_howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ara\_howard](https://www.ruby-forum.com/u/ara_howard)\
**Post date:** [April 13, 2008, 1:12am UTC](https://www.ruby-forum.com/t/hash-slice/134611/2 "2008-04-13T01:12:30Z")

</div>

On Apr 12, 5:00 pm, ara howard [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> hash = self
> keys.inject({}){|returned, key| returned.update key => hash[key]}
> 
> ```
> 
> end  
> end
> 
> ??

Let see… Facets:

class Hash

```
# Returns a new hash with only the given keys.

def slice(*keep_keys)
  h = {}
  keep_keys.each do |key|
    h[key] = fetch(key)
  end
  h
end

# Replaces hash with a new hash having only the given keys.
# This return the hash of keys removed.

def slice!(*keep_keys)
  removed = except(*keep_keys)
  replace(slice(*keep_keys))
  removed
end

```

end

T.

---

<div class="post-metadata">

**Author:** ![ara\_howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ara\_howard](https://www.ruby-forum.com/u/ara_howard)\
**Post date:** [April 13, 2008, 8:15am UTC](https://www.ruby-forum.com/t/hash-slice/134611/3 "2008-04-13T08:15:13Z")

</div>

On Apr 12, 3:00 pm, ara howard [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> hash = self
> keys.inject({}){|returned, key| returned.update key => hash[key]}
> 
> ```
> 
> end  
> end

[http://raa.ruby-lang.org/project/hashslice/](http://raa.ruby-lang.org/project/hashslice/)

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![ara\_howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ara\_howard](https://www.ruby-forum.com/u/ara_howard)\
**Post date:** [April 13, 2008, 2:51am UTC](https://www.ruby-forum.com/t/hash-slice/134611/4 "2008-04-13T02:51:51Z")

</div>

On Apr 12, 2008, at 5:12 PM, Trans wrote:

> ```
> end
> 
> ```
> 
> end
> 
> end

okay, you should add the block form 😉

headers.slice{|k,| k =~ %r/^HTTP\_/}

handy…

cheers.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![ara\_howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ara\_howard](https://www.ruby-forum.com/u/ara_howard)\
**Post date:** [April 13, 2008, 10:59am UTC](https://www.ruby-forum.com/t/hash-slice/134611/5 "2008-04-13T10:59:19Z")

</div>

On Sun, Apr 13, 2008 at 2:51 AM, ara.t.howard [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > ```
> > h = {}
> > removed = except(*keep_keys)
> > 
> > ```
> 
> h.h. the 14th dalai lama

Hmm maybe it is a more consistent approach not to add a block to slice.

FWIAC, I use hselect, kselect and vselect, the simplified code  
goes like this (1) of course  
def kselect &blk  
hselect{ |k,| blk.call(k) }  
end  
def vselect &blk  
hselect{ |\_,v| blk.call(v) }  
end  
def hselect &blk  
#almost Tom’s code of course  
end

You might write  
h.slice(\*[\*1…10]){|k,v| whatever k, v }  
which indeed I like

I would write  
h.slice(\*[\*1…10]).hselect{|k,v| whatever k, v}  
which indeed I like less, _but_ I dislike the inconsistency of #select  
now.

The ideal solution might be to add a block to Enumerator#select too,  
what about that?

Cheers  
Robert

(1) simplified because the magic dot notation implementing proxy is of  
no interest here.

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

Whereof one cannot speak, thereof one must be silent.  
Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![ara\_howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ara\_howard](https://www.ruby-forum.com/u/ara_howard)\
**Post date:** [April 13, 2008, 2:23pm UTC](https://www.ruby-forum.com/t/hash-slice/134611/6 "2008-04-13T14:23:06Z")

</div>

On Sun, Apr 13, 2008 at 12:04 PM, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > end  
> > > > end  
> > > > –  
> > > > end  
> > > > hash.keys.select  
> > > > hash.values.select  
> > > > We were talking about getting sliced hashes not sliced key or value  
> > > > arrays. Maybe in the future there will be a saying  
> > > > “the best thing since the invention of sliced hashes by Ara T. Howard”  
> > > > the real inventor gets always forgotten sorry for Facets I can not  
> > > > change future history 😉  
> > > > The ideal solution might be to add a block to Enumerator#select too,  
> > > > what about that?
> 
> Hmm… I don’t understand Enumerator#select comes from  
> Enumerable#select and already takes a block.  
> Stupid me _again_ I meant Array#slice. I feel that my brain is  
> terribly disconnected from the keyboard, (I believe that Scotty was  
> right when talking into the mouse LOL). Well proofreading my posts  
> would be an option though :).

I will express myself in Ruby

assert\_nothing\_raised (“You see it would be nice that Array#slice and  
Hash#slice had (almost) the same interface”) do  
[\*1…100].slice(1…10){ |x| x.zero? }  
end

## Sorry R.

[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

Whereof one cannot speak, thereof one must be silent.  
Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![ara\_howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ara\_howard](https://www.ruby-forum.com/u/ara_howard)\
**Post date:** [April 13, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/hash-slice/134611/7 "2008-04-13T12:05:08Z")

</div>

On Apr 13, 4:58 am, “Robert D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > def slice(\*keep\_keys)  
> > > def slice!(\*keep\_keys)
> 
> Hmm maybe it is a more consistent approach not to add a block to slice.  
> def hselect &blk  
> #almost Tom’s code of course  
> end

Why not?

hash.keys.select  
hash.values.select

> You might write  
> h.slice(\*[\*1…10]){|k,v| whatever k, v }  
> which indeed I like
> 
> I would write  
> h.slice(\*[\*1…10]).hselect{|k,v| whatever k, v}  
> which indeed I like less, _but_ I dislike the inconsistency of #select now.
> 
> The ideal solution might be to add a block to Enumerator#select too,  
> what about that?

Hmm… I don’t understand Enumerator#select comes from  
Enumerable#select and already takes a block.

T.

---

<div class="post-metadata">

**Author:** ![ara\_howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ara\_howard](https://www.ruby-forum.com/u/ara_howard)\
**Post date:** [April 13, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/hash-slice/134611/8 "2008-04-13T17:03:55Z")

</div>

On Apr 13, 2008, at 6:22 AM, Robert D. wrote:

> assert\_nothing\_raised (“You see it would be nice that Array#slice and  
> Hash#slice had (almost) the same interface”) do  
> [\*1…100].slice(1…10){ |x| x.zero? }  
> end

now that does make sense 😉

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![ara\_howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ara\_howard](https://www.ruby-forum.com/u/ara_howard)\
**Post date:** [April 13, 2008, 5:02pm UTC](https://www.ruby-forum.com/t/hash-slice/134611/9 "2008-04-13T17:02:08Z")

</div>

On Apr 13, 2008, at 4:04 AM, Trans wrote:

> Why not?
> 
> hash.keys.select  
> hash.values.select

because the goal is to return a hash, not an array… i think some  
wires got crossed though 😉

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![ara\_howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ara\_howard](https://www.ruby-forum.com/u/ara_howard)\
**Post date:** [April 14, 2008, 2:23am UTC](https://www.ruby-forum.com/t/hash-slice/134611/10 "2008-04-14T02:23:08Z")

</div>

On Apr 12, 2008, at 11:14 PM, Daniel B. wrote:

> [http://raa.ruby-lang.org/project/hashslice/](http://raa.ruby-lang.org/project/hashslice/)

Ack, apologies to those who followed the link off that page to a 404.  
Fixed now.
