Hash question


D

Damjan Rems

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
 
Ad

Advertisements

A

Axel Etzold

Hi ---
-------- Original-Nachricht --------
Datum: Mon, 5 May 2008 22:10:31 +0900
Von: Damjan Rems <[email protected]>
An: (e-mail address removed)
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

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


Best regards,

Axel
 
R

Robert Klemme

2008/5/5 Axel Etzold said:
Hi ---
-------- Original-Nachricht --------


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
 
Y

Yossef Mendelssohn

2008/5/5 Axel Etzold <[email protected]>:






Unfortunately these return Array instead of Hash.

Kind regards

robert

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.
 
A

Axel Etzold

Dear Robert,

-------- Original-Nachricht --------
Datum: Mon, 5 May 2008 22:42:03 +0900
Von: "Robert Klemme" <[email protected]>
An: (e-mail address removed)
Betreff: Re: Hash question
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 [email protected] returns an array.


Best regards,

Axel
 
Ad

Advertisements

D

Damjan Rems

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
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top