From: Axel Etzold Date: 2008-05-06T02:35:14+09:00 Subject: Re: Hash question Dear Robert, -------- Original-Nachricht -------- > Datum: Mon, 5 May 2008 22:42:03 +0900 > Von: "Robert Klemme" > An: ruby-talk@ruby-lang.org > Betreff: Re: Hash question > 2008/5/5 Axel Etzold : > > Hi --- > > -------- Original-Nachricht -------- > > > Datum: Mon, 5 May 2008 22:10:31 +0900 > > > Von: Damjan Rems > > > 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. 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 -- 249 Spiele f�r nur 1 Preis. Die GMX Spieleflatrate schon ab 9,90 Euro. Neu: Asterix bei den Olympischen Spielen: http://flat.games.gmx.de