I have the following ruby function, and when I call it `deg_to_dir 270′ the case statement never matches, when it should return W. I have been pulling my hair out and short of converting the entire thing to IF statements I don’t have a next step.
def deg_to_dir deg
# used logic from http://www.csgnetwork.com/degrees2direct.html
d = deg.to_f
dir = "#{d}°"
case d
when d >= 0 && d <= 11.25,
d > 348.75 && d <= 360
dir = "N"
when d > 11.25 && d <= 33.75
dir = "NNE"
when d > 33.75 && d <= 56.25
dir = "NE"
when d > 56.25 && d <= 78.75
dir = "ENE"
when d > 78.75 && d <= 101.25
dir = "E"
when d > 101.25 && d <= 123.75
dir = "ESE"
when d > 123.75 && d <= 146.25
dir = "SE"
when d > 146.25 && d <= 168.75
dir = "SSE"
when d > 168.75 && d <= 191.25
dir = "S"
when d > 191.25 && d <= 213.75
dir = "SSW"
when d > 213.75 && d <= 236.25
dir = "SW"
when d > 236.25 && d <= 258.75
dir = "WSW"
when d > 258.75 && d <= 281.25
dir = "W"
when d > 281.25 && d <= 303.75
dir = "WNW"
when d > 303.75 && d <= 326.25
dir = "NW"
when d > 326.25 && d <= 348.75
dir = "NNW"
end
dir
end
What you’re trying to do is an
if elsifpattern. If you’re trying to use acase whenpattern should be using ranges:Every time the parser comes to a when statment it executes
case_statement === when_statement. All of yourwhenstatements evaluate to a boolean, which will never equal a degree.