Created
November 10, 2015 23:11
-
-
Save edgar/9ba8c70169c98246bc0b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 InheritsFromHash | |
def select(*args, &block) | |
dup.tap { |hash| hash.select!(*args, &block) } | |
end | |
def reject(*args, &block) | |
dup.tap { |hash| hash.reject!(*args, &block) } | |
end | |
end | |
class HashSubclass < Hash | |
include InheritsFromHash | |
def foo | |
self.reject {|k,v| k[:foo]} | |
end | |
end | |
obj = HashSubclass.new | |
new_obj = obj.foo | |
new_obj.class | |
# In ruby 1.9.3 | |
=> HashSubclass | |
# In Ruby 2.2+ | |
=> HashSubclass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment