Skip to content

Instantly share code, notes, and snippets.

@davidbalbert
Last active December 21, 2015 02:38
Show Gist options
  • Save davidbalbert/6235918 to your computer and use it in GitHub Desktop.
Save davidbalbert/6235918 to your computer and use it in GitHub Desktop.
Syntax idea: conditionals in hash literals
# If the conditionals evaluate to false, the key wouldn't be present in the hash
Person.new(
first_name: params[:first_name],
last_name: params[:last_name],
age: params[:age] if params[:age].to_i > 18,
bio: params[:bio] unless params[:bio].blank?)
# This would be equivalent to
person_hash = {
first_name: params[:first_name],
last_name: params[:last_name]
}
person_hash[:age] = params[:age] if params[:age].to_i > 18
person_hash[:bio] = params[:bio] unless params[:bio].blank?
Person.new(person_hash)
@davidbalbert
Copy link
Author

Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment