Skip to content

Instantly share code, notes, and snippets.

@spint
Created August 14, 2009 09:16
Show Gist options
  • Save spint/167733 to your computer and use it in GitHub Desktop.
Save spint/167733 to your computer and use it in GitHub Desktop.
# Present
"brain".present?
#=> true
"".present?
#=> false
#Hash#fetch
items = { :apples => 2, :oranges => 3 }
#=> items = {:apples=>2, :oranges=>3}
items.fetch(:apples)
#=> 2
items.fetch(:bananas) { |key| "We don't carry #{key}!"}
#=> We don't carry bananas!
# Hash#new with a block
smash = Hash.new { |hash, key| hash[key] = "a #{key} just got SMASHED!" }
#=> {}
smash[:plum] = "cannot smash."
#=> {:plum=>"cannot smash."}
smash[:watermelon]
#=> {:plum=>"cannot smash.", :watermelon=>"a watermelon just got SMASHED!"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment