Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Hash pairs at?

Reply
Thread Tools

Hash pairs at?

 
 
Trans
Guest
Posts: n/a
 
      01-21-2007
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.


 
Reply With Quote
 
 
 
 
Chris Carter
Guest
Posts: n/a
 
      01-21-2007
On 1/20/07, Trans <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
William James
Guest
Posts: n/a
 
      01-21-2007
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}

 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      01-21-2007
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

 
Reply With Quote
 
gga
Guest
Posts: n/a
 
      01-21-2007

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 }

 
Reply With Quote
 
gga
Guest
Posts: n/a
 
      01-21-2007

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

 
Reply With Quote
 
dblack@wobblini.net
Guest
Posts: n/a
 
      01-21-2007
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)

 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      01-21-2007
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

 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      01-21-2007
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.

 
Reply With Quote
 
dblack@wobblini.net
Guest
Posts: n/a
 
      01-21-2007
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)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
replace lines in a file using hash key-value pairs Johan Martinez Ruby 3 05-22-2011 03:07 AM
function to select only certain key/value pairs from hash? Aryk Grosz Ruby 13 11-18-2008 12:39 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
sort multi-key hash by value and print out with key value pairs Antonio Quinonez Perl Misc 2 08-14-2003 10:56 PM



Advertisments