0

What is the quickest way in Ruby to retrieve the correct hash by language from the following nested hash:

COUNTRIES = {
  "DE" => {:currency => "EUR", :tax_rates => [19, 7], :languages => ["de-de"]},
  "US" => {:currency => "USD", :tax_rates => [10], :languages => ["en-us"]}
}

I managed to retrieve the correct hash by providing the currency:

COUNTRIES.select{|key, hash| hash[:currency] == "USD" }

But how can I query by a language, for example by en-us?

Thanks for any help.

1 Answer 1

3

COUNTRIES.select{|key, hash| hash[:languages].include?("en-us")}

of note, if you're only looking for a singular result, it's cheaper to use #detect than it is to use #select.first

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.