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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:04:19+00:00 2026-06-15T17:04:19+00:00

I have different hashes containing units, grouped into unit types. My code aims to

  • 0

I have different hashes containing units, grouped into unit types. My code aims to determine which unit type should be returned for further processing. However, a lot of duplication is going on when each list is examined. The first if is doing the exact same as the first elsif. How do I DRY the code up the best possible way?

from_unit = "gr"
to_unit = "kg"

WEIGHT = {
"gr" => 1000.0,
"kg" => 1.0,
}

MEASURE = {
"mm" => 1000.0,
"cm" => 100.0,
"m" => 1.0
}

if WEIGHT.has_key?(from_unit) or WEIGHT.has_key?(to_unit)
  if WEIGHT.has_key?(from_unit) && WEIGHT.has_key?(to_unit)
    return WEIGHT
  elsif WEIGHT.has_key?(from_unit)
    raise RuntimeError, "#{to_unit} is not a known unit"
  else
    raise RuntimeError, "#{from_unit} is not a known unit"
  end
elsif MEASURE.has_key?(from_unit) or MEASURE.has_key?(to_unit)
  if MEASURE.has_key?(from_unit) && MEASURE.has_key?(to_unit)
    return WEIGHT
  elsif MEASURE.has_key?(from_unit)
    raise RuntimeError, "#{to_unit} is not a known unit"
  else
    raise RuntimeError, "#{from_unit} is not a known unit"
  end
else
  raise RuntimeError, "You can't convert #{from_unit} into #{to_unit}"
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-06-15T17:04:20+00:00Added an answer on June 15, 2026 at 5:04 pm

    Go for simplicity, this snippet does less checks than yours (are really that necessary?), but gets the job done:

    def get_table(from_unit, to_unit)
      [WEIGHT, MEASURE].detect do |table|
        table[from_unit] && table[to_unit]
      end or fail("You can't convert #{from_unit} into #{to_unit}")
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have different urls that points to the same code www.url1.com www.url2.com I need
I have different projects written in .NET 3.5 and some unit test projects to
I have an application in which I have different activities. In 1 activity, I
Up until recently, I've been storing multiple values into different hashes with the same
I'm using ruby 1.9.3 and I need to compare two hashes that have different
I have a database table (MySQL 5) which stores file hashes and filenames. I
I have an array of hashes, many of which have shared keys. I would
I have rows of hashes imported from several different XML database dumps that look
I have a hash of hashes (@post) and I want to save it into
I have an array of hashes that I want to turn into a table,

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.