I have a class that uses Enumerable
and Forwardable
mixins. The things is that even though #each
has been implemented (or delegated), #member?
(which also comes with Enumerable
) is not working properly.
require "forwardable"
class RestrictedHash
include Enumerable
extend Forwardable
def_delegators :@hash, :[], :[]=, :each
def initialize
@hash = {}
end
end
r_h = RestrictedHash.new
r_h[:a] = []
r_h.member?(:a) #=> false
r_h.member?(:a, []) #=> Wrong number of arguments (2 for 1)
r_h.member?([:a, []]) #=> true
h = {}
h[:a] = []
h.member?(:a) #=> true
h.member?([:a, []]) #=> false
Any ideas of why I am getting this difference in behavior?
The reason for this is that the
each
method on aHash
yields pairs of key and value so for your example@hash
:This means that when the implementation of
member?
inEnumerable
useseach
to check if the value specified is a member it will successfully find([:a, []]
but not the key:a
on its own.In the
Hash
class itselfmember?
is implemented to call rb_hash_has_key.