Skip to content

Instantly share code, notes, and snippets.

@rachidcalazans
Last active January 2, 2018 13:06
Show Gist options
  • Save rachidcalazans/f9be2e19e4b2ec14e41bdd25c8af9245 to your computer and use it in GitHub Desktop.
Save rachidcalazans/f9be2e19e4b2ec14e41bdd25c8af9245 to your computer and use it in GitHub Desktop.
Example II for the Post - avoiding-conditionals
def extract_params(state, params)
if state == :in_progress
return params.select { |k, v| [:key_a, :key_b].include?(k) }
elsif state == :done
return params.select { |k, v| [:key_c, :key_d].include?(k) }
elsif state == :on_review
return params.select { |k, v| [:key_e, :key_f].include?(k) }
end
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