Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5957767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:26:54+00:00 2026-05-22T18:26:54+00:00

I have the following ruby function, and when I call it `deg_to_dir 270′ the

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:26:55+00:00Added an answer on May 22, 2026 at 6:26 pm

    What you’re trying to do is an if elsif pattern. If you’re trying to use a case when pattern should be using ranges:

    case d
    when (0..11.25)
      dir = "N"
    when (11.25..33.75)
      dir = "NE"
      # etc...
    end
    

    Every time the parser comes to a when statment it executes case_statement === when_statement. All of your when statements evaluate to a boolean, which will never equal a degree.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function in Ruby that decrypts a bit of data: def
Hi. I'm working on a Ruby C++ extension, I have the following function, in
I have implemented Ruby C extension(i.e. Invoking C function from ruby script) Following is
Let's say I have the following ruby code : def use_object(object) puts object.some_method end
I have the following code in Ruby. I want to convert this code into
If I have the following piece of Ruby code: class Blah def self.bleh @blih
I have the following code: #!/usr/bin/ruby class Person def self.speak p = self.new puts
I have the following XML document: <projects> <project> <name>Shockwave</name> <language>Ruby</language> <owner>Brian May</owner> <state>New</state> <startDate>31/10/2008
I am trying to install ruby-mp3info. I have the following URL from which I
This is not a ruby/rails project deploy. I have the following situation and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.