From: 7stud -- Date: 2011-07-15T19:52:42+09:00 Subject: Re: Elegant selection of closest number? Philip Rhoades wrote in post #1010919: > People, buckets = [ 0.1, 0.2, 0.3, 0.4, 0.5, ] data = [ 0.00, 0.14, 0.19, 0.24, 0.25, 0.41, 1.00, ] results = Hash.new {|hash, key| hash[key] = []} data.each do |num| rounded = num.round(1) first = buckets.first last = buckets.last if rounded < first results[first] << num elsif rounded > last results[last] << num else results[rounded] << num end end p results --output:-- {0.1=>[0.0, 0.14], 0.2=>[0.19, 0.24], 0.3=>[0.25], 0.4=>[0.41], 0.5=>[1.0]} -- Posted via http://www.ruby-forum.com/.