Ruby, value bucketing, beautify code

前端 未结 5 638
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-25 02:04

So I have this code:

def self.age_to_bucket(age)
  age = age.to_i

  if age >= 0 && age <= 12
    1
  elsif age >= 13 && age <= 17
          


        
5条回答
  •  悲&欢浪女
    2021-01-25 02:26

    irb(main):010:0> a = {1 => 0..12, 2 => 13..17} # insert other values here
    => {1=>0..12, 2=>13..17}
    irb(main):011:0> age = 16
    => 16
    irb(main):012:0> a.keys.find {|k| a[k].include?(age) }
    => 2
    

提交回复
热议问题