Mark
Mark

Reputation: 385

Ruby ranges issue

I am searching a range here but it seems to give me blank results.

numbers = ["03","03","06","06","06","07","09","10"]

numbers.each do |n|
    result = case n
        when 1..5 then "Jan"
        when 6..10 then "Feb"
    end
    puts result
end

Any help? Thanks

Upvotes: 0

Views: 74

Answers (1)

Kassym Dorsel
Kassym Dorsel

Reputation: 4843

The range you have is an integer range. For this to work your input also needs to be in integers :

numbers = ["03","03","06","06","06","07","09","10"]
numbers.each do |n|
    result = case n.to_i
        when 1..5 then "Jan"
        when 6..10 then "Feb"
    end
    puts result
end

You could also leave it as strings and do it this way :

numbers = ["03","03","06","06","06","07","09","10"]
numbers.each do |n|
    result = case n
        when '01'..'05' then "Jan"
        when '06'..'10' then "Feb"
    end
    puts result
end

Upvotes: 3

Related Questions