[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Hash pairs at?

Trans

1/21/2007 1:11:00 AM

Seesm like there should be a mehtod for this:

h = { :a=>1, :b=>2, :c=>3 }

h.what_method(:a, :c) #=> { :a=>1, :c=>3 }
^^^^^^^^^^^

Or is there some other simple way we're supposed to do this?

T.


25 Answers

Chris Carter

1/21/2007 1:15:00 AM

0

On 1/20/07, Trans <transfire@gmail.com> wrote:
> Seesm like there should be a mehtod for this:
>
> h = { :a=>1, :b=>2, :c=>3 }
>
> h.what_method(:a, :c) #=> { :a=>1, :c=>3 }
> ^^^^^^^^^^^
>
> Or is there some other simple way we're supposed to do this?
>
> T.
>
>
>
http://concentrationstudios.com/2006/12/20/12-days-of-stupid-r...

Check out Day 11

--
Chris Carter
concentrationstudios.com
brynmawrcs.com

William James

1/21/2007 1:56:00 AM

0

Trans wrote:
> Seesm like there should be a mehtod for this:
>
> h = { :a=>1, :b=>2, :c=>3 }
>
> h.what_method(:a, :c) #=> { :a=>1, :c=>3 }
> ^^^^^^^^^^^
>
> Or is there some other simple way we're supposed to do this?
>
> T.

h = { :a,1, :b,2, :c,3 }
==>{:c=>3, :a=>1, :b=>2}
h.reject{|k,v| ![:a,:c].include? k}
==>{:c=>3, :a=>1}

Joel VanderWerf

1/21/2007 8:06:00 AM

0

Trans wrote:
> Seesm like there should be a mehtod for this:
>
> h = { :a=>1, :b=>2, :c=>3 }
>
> h.what_method(:a, :c) #=> { :a=>1, :c=>3 }
> ^^^^^^^^^^^
>
> Or is there some other simple way we're supposed to do this?
>
> T.
>

h = { :a=>1, :b=>2, :c=>3 }

class Hash
def restrict(*keys)
keys.inject({}) {|h,k| h[k] = self[k]; h}
# alternative:
#Hash[*keys.zip(values_at(*keys)).flatten]
end
end

p h.restrict(:a, :c) #=> { :a=>1, :c=>3 }

Maybe there should be something like this in the core?

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

gga

1/21/2007 1:15:00 PM

0


Trans ha escrito:
> Seesm like there should be a mehtod for this:

No.

>
> Or is there some other simple way we're supposed to do this?
>
> T.

[:a, :c].inject({}) { |s, x| s[x] = h[x]; s }

gga

1/21/2007 1:41:00 PM

0


gga wrote:
> Trans ha escrito:
> > Seesm like there should be a mehtod for this:
>
> No.
>

That being said, Hash DOES seem to be buggy and missing a proper
find_all method.

irb> h.find_all { |k,v| [:a,:c].include?(k) }
=> [[:c, 3], [:a, 1]] # incorrect, it is returning an array, not a
hash

irb> h.reject { |k,v| ![:a,:c].include?(k) }
{:c=>3, :a=>1} # correct

dblack

1/21/2007 2:00:00 PM

0

Gavin Kistner

1/21/2007 3:10:00 PM

0

Trans wrote:
> Seesm like there should be a mehtod for this:
>
> h = { :a=>1, :b=>2, :c=>3 }
>
> h.what_method(:a, :c) #=> { :a=>1, :c=>3 }
> ^^^^^^^^^^^

# h = { :a=>'a', :b=>'b', :c=>'c' }
# p h.entries( :a, :c, :d )
# #=> { :d=>nil, :a=>'a', :c=>'c' }
class Hash
def entries( *keys )
self.class[ *keys.zip( values_at( *keys ) ).flatten ]
end
end

Gavin Kistner

1/21/2007 3:14:00 PM

0

Phrogz wrote:
> Trans wrote:
> > Seems like there should be a mehtod for this:
> class Hash
> def entries( *keys )
> self.class[ *keys.zip( values_at( *keys ) ).flatten ]
> end
> end

Oh, and yes: I'd like a method like this in the core, also. Preferably
with a more elegant, faster implementation than the above. I find this
quite useful sometimes. A recent example that comes to mind is taking
the params hash in Rails and specifying a specific subset of the
populated values to pass on to another method.

dblack

1/21/2007 3:31:00 PM

0

Gavin Kistner

1/21/2007 3:36:00 PM

0

dbl...@wobblini.net wrote:
> I'll put in a plug here for flattenx, which lets you flatten by any
> number of levels so that you could do the above without the
> over-flattening vulnerability.
>
> http://raa.ruby-lang.org/project...

Yes, yes! I've used this locally, and wanted to rely on this remotely,
many times!

This, too, should be in the core (particularly since it requires
compilation, and is not a pure-ruby solution.)