开发者

Extracting numeric range "values" from a Ruby Hashtable or JSon object

开发者 https://www.devze.com 2023-04-07 21:02 出处:网络
I am quite new to Ruby (1.8.7), and would like to be able to extract values from开发者_运维技巧 a Hashtable which are within a specific numeric range (without having to iterate through the values).

I am quite new to Ruby (1.8.7), and would like to be able to extract values from开发者_运维技巧 a Hashtable which are within a specific numeric range (without having to iterate through the values).

If I have Hashtable with:

my_hash = {
  55 => {:value=>61, :rating=>147},
  89 => {:value=>72, :rating=>200},
  78 => {:value=>64, :rating=>300}
}

How would I parse the Hashtable so the rating=>300 was returned (if a range of 250..350) was specified? Alternatively I will sort the Hashtable on the rating value and iterate checking each value individually.

Thanks,

Miles.


To return my_hash entries within a range of ratings:

my_hash.select { |k,v| (250..350).include?(v[:rating]) }
0

精彩评论

暂无评论...
验证码 换一张
取 消