2008/5/5 Axel Etzold <AEtzold / gmx.de>:
> Hi ---
>  -------- Original-Nachricht --------
>  > Datum: Mon, 5 May 2008 22:10:31 +0900
>  > Von: Damjan Rems <d_rems / yahoo.com>
>  > An: ruby-talk / ruby-lang.org
>  > Betreff: Hash question
>
>  >  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
>  > --
>  > Posted via 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

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

-- 
use.inject do |as, often| as.you_can - without end