-
-
Save havenwood/a69cf958f5bc42b3200f14a91543c1bc to your computer and use it in GitHub Desktop.
Example refinement usage for alex`` in the #ruby channel on freenode.net
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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