Skip to content

Instantly share code, notes, and snippets.

@rachidcalazans
Last active January 2, 2018 13:06
Show Gist options
  • Save rachidcalazans/7531b9aa9f2fcd7105a851851d8f5982 to your computer and use it in GitHub Desktop.
Save rachidcalazans/7531b9aa9f2fcd7105a851851d8f5982 to your computer and use it in GitHub Desktop.
Example II Refactored for the Post - avoiding-conditionals
def extract_params(state, params)
state_keys = {
in_progress: %I[key_a key_b],
done: %I[key_c key_d],
on_review: %I[key_e key_f]
}
params.select { |k, v| state_keys[state].include?(k) }
end
params = { key_a: 1, key_b: 2, key_c: 3, key_d: 4, key_e: 5, key_f: 6 }
extract_params(:in_progress, params) #=> { key_a: 1, key_b: 2 }
extract_params(:done, params) #=> { key_c: 3, key_d: 4 }
extract_params(:on_review, params) #=> { key_e: 5, key_f: 6 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment