Ruby on rails yml中未知数量的节点-如何解析?
在一个应用程序中,我有:Ruby on rails yml中未知数量的节点-如何解析?,ruby-on-rails,ruby,ruby-on-rails-4,yaml,Ruby On Rails,Ruby,Ruby On Rails 4,Yaml,在一个应用程序中,我有: def get_debt_for_month(api_requests_count) case api_requests_count when 0..50 5 when 50..100 9 when 100..200 13 # and so on end end 显然,硬编码不是一个好主意。我该怎么做?我认为最好的方法是将价格移动到yml文件中,但考虑到案例数量未知且可能会发生变化,为什么我要动态解析它呢?如果范围内没有
def get_debt_for_month(api_requests_count)
case api_requests_count
when 0..50
5
when 50..100
9
when 100..200
13
# and so on
end
end
显然,硬编码不是一个好主意。我该怎么做?我认为最好的方法是将价格移动到yml文件中,但考虑到案例数量未知且可能会发生变化,为什么我要动态解析它呢?如果范围内没有间隙,就使用起始值
# data.yml
0: 5
51: 9
101: 13
然后你的方法
def get_debt_for_month(api_request_count)
thresholds = YAML.load_file('data.yml')
thresholds.sort.select{|e| e[0] <= api_request_count}.last[1]
end
def获取月债务(api请求计数)
thresholds=YAML.load_文件('data.yml'))
thresholds.sort.select{| e | e[0]