Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Hash pairs at? (http://www.velocityreviews.com/forums/t837390-hash-pairs-at.html)

Trans 01-21-2007 01:11 AM

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.



Chris Carter 01-21-2007 01:15 AM

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


William James 01-21-2007 01:55 AM

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}


Joel VanderWerf 01-21-2007 08:05 AM

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


gga 01-21-2007 01:14 PM

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 }


gga 01-21-2007 01:41 PM

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


dblack@wobblini.net 01-21-2007 02:00 PM

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)


Phrogz 01-21-2007 03:10 PM

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


Phrogz 01-21-2007 03:13 PM

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.


dblack@wobblini.net 01-21-2007 03:31 PM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57