![]() |
Hash pairs at?
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. |
Re: Hash pairs at?
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...id-ruby-tricks Check out Day 11 -- Chris Carter concentrationstudios.com brynmawrcs.com |
Re: Hash pairs at?
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} |
Re: Hash pairs at?
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 |
Re: Hash pairs at?
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 } |
Re: Hash pairs at?
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 |
Re: Hash pairs at?
Hi --
On Sun, 21 Jan 2007, gga wrote: > > 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 It's not incorrect or buggy; that's the way Enumerable#find_all works. In addition to being Enumerables themselves, arrays serve as the common container for the results of lots of different Enumerables. It could work otherwise (at least for hashes, though not in the general Enumerable case), but it works this way by design, not by accident. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com) |
Re: Hash pairs at?
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 |
Re: Hash pairs at?
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. |
Re: Hash pairs at?
Hi --
On Mon, 22 Jan 2007, Phrogz wrote: > 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. 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/flattenx/ David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com) |
| All times are GMT. The time now is 12:34 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.