Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created February 20, 2018 19:00
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 havenwood/a69cf958f5bc42b3200f14a91543c1bc to your computer and use it in GitHub Desktop.
Save havenwood/a69cf958f5bc42b3200f14a91543c1bc to your computer and use it in GitHub Desktop.
Example refinement usage for alex`` in the #ruby channel on freenode.net
module HashSliceBackport
refine Hash do
def slice *keys
keys.each_with_object({}) do |key, hash|
hash[key] = self[key] if key?(key)
end
end
end
end
class Node
using HashSliceBackport
def example
{a: 100, b: 200, c: 300}.slice(:b, :a)
end
end
Node.new.example
#=> {:b=>200, :a=>100}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment