# Hash question

**URL:** <https://www.ruby-forum.com/t/hash-question/136684>\
**Category:** Ruby\
**Created:** [May 5, 2008, 3:10pm UTC](https://www.ruby-forum.com/t/hash-question/136684 "2008-05-05T15:10:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [May 5, 2008, 3:10pm UTC](https://www.ruby-forum.com/t/hash-question/136684/1 "2008-05-05T15:10:30Z")

</div>

I would like to return only part of hash as hash object.

Example:  
h = { “a” =\> 100, “b” =\> 200, “c” =\> 300 }  
result = h.somemethod(‘a’,‘c’)

# should result in { “a” =\> 100, “c” =\> 200}

Is this already build in Hash object or should I start and code it.

by  
TheR

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [May 5, 2008, 3:32pm UTC](https://www.ruby-forum.com/t/hash-question/136684/2 "2008-05-05T15:32:05Z")

</div>

Hi —  
-------- Original-Nachricht --------

> Datum: Mon, 5 May 2008 22:10:31 +0900  
> Von: Damjan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> An: removed\_email\_address@domain.invalid  
> Betreff: Hash question

> ## by TheR
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

you can use Hash#select or Hash#delete or Hash#delete\_if for tasks like  
this.  
See some examples in the documentation for Hash:

[http://www.ruby-doc.org/core/classes/Hash.html#M002898](http://www.ruby-doc.org/core/classes/Hash.html#M002898)

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [May 5, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/hash-question/136684/3 "2008-05-05T15:42:50Z")

</div>

2008/5/5 Axel E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > h = { “a” =\> 100, “b” =\> 200, “c” =\> 300 }
> 
> you can use Hash#select or Hash#delete or Hash#delete\_if for tasks like  
> this.  
> See some examples in the documentation for Hash:
> 
> [class Hash - RDoc Documentation](http://www.ruby-doc.org/core/classes/Hash.html#M002898)

Unfortunately these return Array instead of Hash.

You can do something like this:

irb(main):006:0\> ha = { “a” =\> 100, “b” =\> 200, “c” =\> 300 }  
=\> {“a”=\>100, “b”=\>200, “c”=\>300}  
irb(main):007:0\> sel = %w{a c}  
=\> [“a”, “c”]

irb(main):008:0\> ha.inject({}) {|h,(k,v)| h[k]=v if sel.include? k;h}  
=\> {“a”=\>100, “c”=\>300}

irb(main):012:0\> Hash[\*ha.select {|k,v| sel.include? k}.flatten]  
=\> {“a”=\>100, “c”=\>300}

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [May 5, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/hash-question/136684/4 "2008-05-05T17:15:31Z")

</div>

On May 5, 8:42 am, “Robert K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > TheR
> 
> Kind regards
> 
> robert
> 
> –  
> use.inject do |as, often| as.you\_can - without end

Fortunately, this bone-headed behavior will be fixed in 1.9. Until  
then, you can do what many others do and use Hash#reject with negative  
logic.

In other words, instead of

```
{ "a" => 100, "b" => 200, "c" => 300 }.select { |k, v| %w[a

```

c].include?(k) }

use

```
{ "a" => 100, "b" => 200, "c" => 300 }.reject { |k, v| !%w[a

```

c].include?(k) }

Yes, I like this about as much as you’d think.

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [May 5, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/hash-question/136684/5 "2008-05-05T19:35:58Z")

</div>

Dear Robert,

-------- Original-Nachricht --------

> Datum: Mon, 5 May 2008 22:42:03 +0900  
> Von: “Robert K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> An: removed\_email\_address@domain.invalid  
> Betreff: Re: Hash question

> > > Example:  
> > > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > you can use Hash#select or Hash#delete or Hash#delete\_if for tasks like  
> > this.  
> > See some examples in the documentation for Hash:
> > 
> > [class Hash - RDoc Documentation](http://www.ruby-doc.org/core/classes/Hash.html#M002898)

> Unfortunately these return Array instead of Hash.

Hash#delete\_if does indeed return a Hash (and I’m using ruby 1.8.6  
(2007-06-07 patchlevel 36) [i486-linux]):

h = { “a” =\> 100, “b” =\> 200, “c” =\> 300 }  
h.delete\_if {|key, value| key \>= “b” } #=\> {“a”=\>100}

So there’s no need to code yourself here.  
But you’re right insofar as Hash#select returns a value and Hash@delete  
returns an array.

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [May 6, 2008, 8:42am UTC](https://www.ruby-forum.com/t/hash-question/136684/6 "2008-05-06T08:42:25Z")

</div>

Here is what I have done:

class Hash  
def select\_as\_hash(\*args)  
h = {}  
args.each {|k| h[k]=self[k] unless self[k].nil? }  
h  
end  
end

by  
TheR
