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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:21:08+00:00 2026-05-16T22:21:08+00:00

Developing a little survey webapp, ran into problem that deals with ranges for rating

  • 0

Developing a little survey webapp, ran into problem that deals with ranges for rating type questions.
So a rating’s range could be:

1..10
-5..0
-5..5
'a'..'z'
'E'..'M'

and so on

The range is stored as a pair of varchars in database (start and end of range). So range always starts off as a string input.

What is the best way to take these string values and build a Ruby Range accordingly.
I can’t just go value.to_i as this won’t work for string iteration. Having a bunch of if’s seems ugly. Any better way?

Not as important, but worth asking:
Also what if I wanted to make it all work with reversed range? Say 5-to-0 or G-to-A. I know that Ruby doesn’t support reverse range (since it uses succ() to iterate). What would be the best way here?

Thanks in advance!

Update:

Based on Wouter de Bie’s suggestion I’ve settled for this:

def to_int_or_string(str)
  return str.match(/^-?\d+$/) ? str.to_i : str.strip
end

def ratings_array(from, to)
  from = to_int_or_string(from)
  to = to_int_or_string(to)
  from > to ? Range.new(to, from).to_a.reverse : Range.new(from, to).to_a
end

Any thoughts?

  • 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-16T22:21:09+00:00Added an answer on May 16, 2026 at 10:21 pm

    Use Range.new:

    Range.new("a","z")
    => "a".."z"
    
    Range.new(-5,5)
    => -5..5
    

    If you’re varchars contain quotes, you can use eval to get the right ranges:

    from = "'a'"
    to = "'z'"
    eval("Range.new(#{from},#{to})")
    

    Otherwise you could use value.to_i to figure out if it was a number or a string in the varchar:

    a = "x"
    a = (a.to_i == 0 && a != "0") ? a : a.to_i
    => "x"
    
    a = "5"
    a = (a.to_i == 0 && a != "0") ? a : a.to_i
    => 5
    

    Which of course can be nicely extracted into a method:

    def to_int_or_string(value)
      return (value.to_i == 0 && value != "0") ? value : value.to_i
    end
    
    def to_range(from, to)
      return Range.new(to_int_or_string(from), to_int_or_string(to))
    end
    

    To reverse your range, you have to convert it to an array first:

    Range.new("a","g").to_a.reverse
    => ["g", "f", "e", "d", "c", "b", "a"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little Problem with the app that I'm developing. In the App
I'm currently developing a web application and have run into a little problem. I'm
I'm developing a little web service and ran into a detail I don't understand.
For a school project we're developing a game that's a little like Conway's game
I'm facing an architecture problem I didn't manage to solve. I'm developing a little
currently I'm developing an app for WP7 but came across a little problem with
I'm developing an autocompletion functionality (that consists in a little window under the caret
I'm developing a little email sender portal that allows registered users to send some
Ok guys. I've begun developing a little sparetime project that might become big someday.
I'm doing a little research in developing a simple socket server that will run

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.