Skip to content

Instantly share code, notes, and snippets.

@mehdi-farsi
Created March 12, 2018 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehdi-farsi/3afe43430b2c1578462a6c3085f8e8e2 to your computer and use it in GitHub Desktop.
Save mehdi-farsi/3afe43430b2c1578462a6c3085f8e8e2 to your computer and use it in GitHub Desktop.
Create hash with condition
def new_hash(opt1, opt2 = [])
{}.tap do |h|
h[:opt1] = opt1
h[:opt2] = opt2 unless opt2.empty?
end
end
# instead of
def new_hash(opt1, opt2 = [])
h = {
opt1: opt1
}
h[:opt2] = opt2 unless opt2.empty?
h
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment