Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: How to filter in a hash structure to get all the keys which are <>"1"?

Tags:

ruby

I want to find some elegant way to achieve this. Maybe like follwing:

hash={"1"=>"1","2"=>"2"}
r=[]
hash.each do |k,v|
    if k!="1"
       r<<k
    end
end
puts r

Any better way to achieve this?

like image 624
mlzboy Avatar asked Dec 07 '22 00:12

mlzboy


1 Answers

You can use "array difference":

hash.keys - ['1']
#=> ["2"]
like image 135
Mladen Jablanović Avatar answered Dec 28 '22 23:12

Mladen Jablanović