如何写出更好的方法?

时间:2016-07-08 13:53:10

标签: ruby range

用红宝石编写它的更好,更正确的方法是什么? 我需要创建像矩阵一样的东西。

def suggest_by_area_and_price area_range, price_range
    broker_level = case area_range
                       when 0..100
                           return case price_range
                                      when 0..25000
                                          :intern_broker
                                      when 25001..Float::INFINITY
                                          :junior_broker
                                  end
                       when 101..200
                           return case price_range
                                      when 0..18000
                                          :intern_broker
                                      when 18001..30000
                                          :junior_broker
                                      when 30000..Float::INFINITY
                                          :broker
                                  end



                       #....


                       when 1500..Float::INFINITY
                           :senior_broker
                       else
                           :junior_broker
                   end
end

1 个答案:

答案 0 :(得分:0)

map = {
  (1..5) => {
    (100..200) => :foo,
    (201..300) => :bar
  },
  (6..10) => {
    (100..200) => :baz,
    (201..300) => :boo
  }
}
map.find{|k,_| k === 3}.last.find{|k,_| k === 250}.last
# => :bar

但是,正如我在评论中所说,我会把这张地图放在其他地方。