Skip to content

Instantly share code, notes, and snippets.

@leandro
Created June 1, 2010 21:46
Show Gist options
  • Select an option

  • Save leandro/421558 to your computer and use it in GitHub Desktop.

Select an option

Save leandro/421558 to your computer and use it in GitHub Desktop.
class Hash
def hash_from(*keys)
keys.inject({}) { |memo, obj| memo.merge(obj => self[obj]) }
end
end
# example:
# >> a = {:foo => 1, :bar => 2, :foobar => 3}
# => {:bar=>2, :foobar=>3, :foo=>1}
# >> b = a.hash_from(:foo,:bar)
# => {:bar=>2, :foo=>1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment