[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Hash question

Damjan Rems

5/5/2008 1:11:00 PM


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

5 Answers

Axel Etzold

5/5/2008 1:31:00 PM

0

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

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


Best regards,

Axel
--
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/s...

Robert Klemme

5/5/2008 1:42:00 PM

0

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

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

Yossef Mendelssohn

5/5/2008 3:15:00 PM

0



On May 5, 8:42 am, "Robert Klemme" <shortcut...@googlemail.com> wrote:
> 2008/5/5 Axel Etzold <AEtz...@gmx.de>:
>
>
>
> > Hi ---
> > -------- Original-Nachricht --------
> > > Datum: Mon, 5 May 2008 22:10:31 +0900
> > > Von: Damjan Rems <d_r...@yahoo.com>
> > > An: ruby-t...@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 viahttp://www.ruby-....
>
> > 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.ht...
>
> Unfortunately these return Array instead of Hash.
>
> 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.

--
-yossef

Axel Etzold

5/5/2008 5:35:00 PM

0

Dear Robert,

-------- Original-Nachricht --------
> Datum: Mon, 5 May 2008 22:42:03 +0900
> Von: "Robert Klemme" <shortcutter@googlemail.com>
> An: ruby-talk@ruby-lang.org
> Betreff: Re: Hash question

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


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

Damjan Rems

5/6/2008 6:42:00 AM

0


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
--
Posted via http://www.ruby-....