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 8131495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:06:32+00:00 2026-06-06T09:06:32+00:00

I am using Handlebar in my Rails 3.2 jquery mobile application. I am trying

  • 0

I am using Handlebar in my Rails 3.2 jquery mobile application.

I am trying to write a switch case statement inside a Coffeescript method like

Handlebars.registerHelper 'status', (blog) ->
  switch(parseInt(blog.status))
    when [0..20]
      status = "active"
    when [20..40]
      status = "Moderately Active"
    when [40..60]
      status = "Very Active"
    when [60..100]
      status = "Hyper Active"
  return status

I am not getting any result . How to use range in when . Please suggest

  • 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-06-06T09:06:33+00:00Added an answer on June 6, 2026 at 9:06 am

    Your switch won’t work as Cygal notes in the comments (i.e. see issue 1383). A switch is just a glorified if(a == b) construct and you need to be able to say things like:

    a = [1,2,3]
    switch a
    ...
    

    and have it work when you switch on an array. The CoffeeScript designers thought adding a (fragile) special case to handle arrays (which is all [a..b] is) specially wasn’t worth it.

    You can do it with an if:

    Handlebars.registerHelper 'status', (blog) ->
      status = parseInt(blog.status, 10)
      if 0 <= status <= 20
        'Active'
      else if 20 < status <= 40
        'Moderately Active'
      else if 40 < status <= 60
        'Very Active'
      else if 60 < status <= 100
        'Hyper Active'
      else
        # You need to figure out what to say here
    

    Or with short circuiting returns like this:

    Handlebars.registerHelper 'status', (blog) ->
      status = parseInt(blog.status, 10)
      return 'Something...'      if status <=   0
      return 'Active'            if status <=  20
      return 'Moderately Active' if status <=  40
      return 'Very Active'       if status <=  60
      return 'Hyper Active'      if status <= 100
      return 'Something else'    # This return isn't necessary but I like the symmetry
    

    Note that you have three special cases that you need to add strings for:

    1. status < 0.
    2. status > 100.
    3. status is NaN. This case would usually fall under the final “it isn’t less than or equal to 100” branch since NaN => n and NaN <= n are both false for all n.

    Yes, you’re absolutely certain that the status will always fall within the assumed range. On the other hand, the impossible happens all the time software (hence the comp.risks mailing list) and there’s no good reason to leave holes that are so easily filled.

    Also note the addition of the radix argument to the parseInt call, you wouldn’t want a leading zero to make a mess of things. Yes, the radix argument is optional but it really shouldn’t be and your fingers should automatically add the , 10 to every parseInt call you make.

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

Sidebar

Related Questions

I am using Ruby on Rails 3 and I would like to use jQuery
I have created a pdf version of our rails application using the Prawn plugin,
I'm brand new at Ruby. Trying to set up the first application/project using Aptana
I am a newbie in Coffeescript and would like to use the delegate() method
I would like to ask how other more experienced Coffeescript developers are using their
In my Rails application I have an action which creates a XML document using
I am using Rails and jquery with RJS templates to perform various AJAX requests.
I am using the runtime build of handlebars and trying to use a registered
I am trying to disable log rotation, for file handler using, FileHandler fh =
I have an event handler set up using plain javascript like this: myElement.addEventListener('drop', handleDrop,

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.