Skip to content

Instantly share code, notes, and snippets.

@rachidcalazans
Last active January 2, 2018 13:07
Show Gist options
  • Save rachidcalazans/eaeb2498ba697330525ec891c7eedced to your computer and use it in GitHub Desktop.
Save rachidcalazans/eaeb2498ba697330525ec891c7eedced to your computer and use it in GitHub Desktop.
Example IV Refactored for the Post - avoiding-conditionals
def save(state, params)
state_lambda = {
in_progress: -> () { save_in_progress(params) },
done: -> () { save_done(params) },
on_review: -> () { save_on_review(params, :some_extra_param) }
}
state_lambda[state].call()
end
params = { a: 1, b: 2, c: 3 }
save(:in_progress, params) #=> "called save_in_progress"
save(:done, params) #=> "called save_done"
save(:on_review, params) #=> "called save_on_review with extract_params"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment