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
2
elsif age >= 18 && age <= 24
3
elsif age >= 25 && age <= 29
4
elsif age >= 30 && age <= 34
5
elsif age >= 35 && age <= 39
6
elsif age >= 40 && age <= 49
7
elsif age >= 50 && age <= 64
8
elsif age >= 65
9
else
0
end
end
How can I improve this code without losing its readability?
I know I can use #in? with ranges, like this:
if age.in? (0..12)
but #in? is in ActiveSupport, and I’d rather use more independent way.
One way is to use case
Another way would be to eliminate the redundant && in the condition.