0

I have a hash set in my code that declares an elegibility scale:

eligibility_scale = [{family_size: 2, minimum_income: 20161, maximum_income: 38200},
                     {family_size: 3, minimum_income: 25393, maximum_income: 43000},
                     {family_size: 4, minimum_income: 30613, maximum_income: 47750},
                     {family_size: 5, minimum_income: 35845, maximum_income: 51600},
                     {family_size: 6, minimum_income: 41065, maximum_income: 55400},
                     {family_size: 7, minimum_income: 46297, maximum_income: 59250},
                     {family_size: 8, minimum_income: 51517, maximum_income: 63050}]

I'm trying to get the hash result using the family_size as the search parameter.

income_bracket = eligibility_scale.select{|h| h["family_size"] == 4 }
# Expecting: {family_size: 4, minimum_income: 30613, maximum_income: 47750}

After running this code I'm getting an empty array.

Any suggestions on what I'm doing wrong?

3 Answers 3

2

Hash keys in the question are symbols, not strings.

You need to specify the key as symbol:

Replace following:

h["family_size"]

with:

h[:family_size]
Sign up to request clarification or add additional context in comments.

Comments

1

Using select will return an array, first of all, so if your family_size is unique, you can just use find. Otherwise, use select in the same way, but expect to have an array of hashes returned.

Second of all, your hashes are using symbols, not string. Ideally, you're looking at writing something like this:

eligibility_scale.find { |i| i[:family_size] == 4 }
# => {family_size: 4, minimum_income: 30613, maximum_income: 47750}

This is assuming your family size is unique.

Comments

0

You've defined your hash using the key: value syntax which is shorthand for :key => value - meaning the keys are symbols and not strings

eligibility_scale.select {|h| h[:family_size] == 4 }

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.