From: Robert Klemme Date: 2008-04-04T06:25:06+09:00 Subject: Re: using a range as a key On 03.04.2008 23:06, Michael Linfield wrote: > I'm trying to achieve the effect of having a range of integers as a hash > key, possible? > > In effect I've tried: > > range = 12345...12360 > > hash = { range => "success!" } > > hash[12349] This cannot possibly work as 12349 != (12345...12360) > => nil > > I've also tried: > > array = [] > range.each{|x| array << x} > > hash = { array => "success!" } > > hash[12349] > => nil Same story. > Anyone have any advice to point me in a direction? If you *always* want to access via individual numbers you can do irb(main):025:0> range = 12345...12360 => 12345...12360 irb(main):026:0> hash = {} => {} irb(main):027:0> range.each {|i| hash[i] = "success!"} => 12345...12360 irb(main):028:0> hash[12349] => "success!" Although I believe there are more space effective solutions (maybe storing range lower boundaries in an array and doing binary search). It all depends on the problem you are trying to solve. Kind regards robert