Skip to content

Instantly share code, notes, and snippets.

@edgar
Created November 10, 2015 23:11
Show Gist options
  • Save edgar/9ba8c70169c98246bc0b to your computer and use it in GitHub Desktop.
Save edgar/9ba8c70169c98246bc0b to your computer and use it in GitHub Desktop.
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