[Solved] Find the range in which the number belongs, from a range string [closed]


▶ CHECKER = [{:id=>1, :text=>"1-10"},
▷   {:id=>2, :text=>"11-50"}, 
▷   {:id=>3, :text=>"51-200"}, 
▷   {:id=>4, :text=>"201-500"}, 
▷   {:id=>5, :text=>"501-1000"}, 
▷   {:id=>6, :text=>"1001-5000"}, 
▷   {:id=>7, :text=>"5001-10000"}, 
▷   {:id=>8, :text=>"10000+"}] 
▶ def test input, checker = CHECKER
▷   checker.map do |r| 
▷     instance_eval(r[:text].sub(/\+$/, '-Float::INFINITY').sub(/-/, '..'))
▷   end.detect do |r| 
▷     r === input
▷   end.to_s.sub(/\.+/, '-').sub(/\.\.Infinity/, '+')
▷​ end
▶ test 700
#⇒ "501-1000"

The approach above will work for any checker intervals while they conform the syntax.

solved Find the range in which the number belongs, from a range string [closed]