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
eachmethod on aHashyields pairs of key and value so for your example@hash:This means that when the implementation of
member?inEnumerableuseseachto check if the value specified is a member it will successfully find([:a, []]but not the key:aon its own.In the
Hashclass itselfmember?is implemented to call rb_hash_has_key.