« Apple Bug Report Friday: Safari: Canvas element. |  main  | Joel on Interns »

Ruby Observations: Hash#select vs. Hash#reject

Posted by dom on Sun Dec 04 16:47:01 +0100 2005

hash = {:key1 => 'value1',:key2 => 'value2'}
=> {:key1=>"value1", :key2=>"value2"}

hash.select{|k,v| [:key1].include? k}
=> [[:key1, "value1"]]

hash.reject{|k,v| ![:key1].include? k}
=> {:key1=>"value1"}

So the result of an select is an array of 2 element array, where the result of an reject is a hash. Useful but strange. I was already on my way to implement something like Hash#select_by_key because I saw the behaviour of Hash#select.

no responses to 'Ruby Observations: Hash#select vs. Hash#reject'

write a comment.... (textile enabled)
author
email
url
Body