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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:45:26+00:00 2026-05-15T12:45:26+00:00

My objective is to convert form input, like 100 megabytes or 1 gigabyte, and

  • 0

My objective is to convert form input, like “100 megabytes” or “1 gigabyte”, and converts it to a filesize in kilobytes I can store in the database. Currently, I have this:

def quota_convert
  @regex = /([0-9]+) (.*)s/
  @sizes = %w{kilobyte megabyte gigabyte}
  m = self.quota.match(@regex)
  if @sizes.include? m[2]
    eval("self.quota = #{m[1]}.#{m[2]}")
  end
end

This works, but only if the input is a multiple (“gigabytes”, but not “gigabyte”) and seems insanely unsafe due to the use of eval. So, functional, but I won’t sleep well tonight.

Any guidance?

EDIT: ——

All right. For some reason, the regex with (.*?) isn’t working correctly on my setup, but I’ve worked around it with Rails stuff. Also, I’ve realized that bytes would work better for me.

def quota_convert
  @regex = /^([0-9]+\.?[0-9]*?) (.*)/
  @sizes = { 'kilobyte' => 1024, 'megabyte' => 1048576, 'gigabyte' => 1073741824}
  m = self.quota.match(@regex)
  if @sizes.include? m[2].singularize
    self.quota = m[1].to_f*@sizes[m[2].singularize]
  end
end

This catches “1 megabyte”, “1.5 megabytes”, and most other things (I hope). It then makes it the singular version regardless. Then it does the multiplication and spits out magic answers.

Is this legit?

EDIT AGAIN: See answer below. Much cleaner than my nonsense.

  • 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-15T12:45:26+00:00Added an answer on May 15, 2026 at 12:45 pm
    def quota_convert
      @regex = /([0-9]+) (.*)s?/
      @sizes = "kilobytes megabytes gigabytes"
      m = self.quota.match(@regex)
      if @sizes.include? m[2]
        m[1].to_f.send(m[2])
      end
    end
    
    • Added ? for optional plural in the regex.
    • Changed @sizes to a string of plurals.
    • Convert m[1] (the number to a float).
    • Send the message m[2] directly
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

just like my question, How can i convert decimal into octal in objective c?
Can anyone help convert this this actionscript to Objective-c? if(mcMain.y >= stage.stageHeight - mcMain.height)
How can i convert int64_t to NSInteger in Objective-C ? This method returns into
How can you convert the UNIX timestamp (that is in the form of a
How can I convert an NSDate to a Unix timestamp in Objective-C?
How can I convert decomposed unicode character sequences like LATIN SMALL LETTER E +
How can I convert and ASCII decimal integer to an NSString in Objective-C? (Example:
Possible Duplicate: Convert string to float in Objective-C I'd like to convert a string
How do I convert a non-object objective-c opaque data type (like CGImageRef, CGPDFPageRef) opaque
In objective-c, how can I convert an integer (representing seconds) to days, minutes, an

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.