mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
support #to_h, #to_hash in Behavior#==
This commit is contained in:
parent
a91028bce1
commit
ce7dcf8473
1 changed files with 18 additions and 11 deletions
|
@ -62,17 +62,18 @@ module Pry::Config::Behavior
|
|||
end
|
||||
|
||||
def merge!(other)
|
||||
other = if other.respond_to?(:to_h)
|
||||
other.to_h
|
||||
elsif other.respond_to?(:to_hash)
|
||||
other.to_hash
|
||||
end
|
||||
other = try_convert_to_hash(other)
|
||||
raise TypeError, "unable to convert argument into a Hash" unless other
|
||||
other.each do |key, value|
|
||||
self[key] = value
|
||||
end
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
to_h == try_convert_to_hash(other)
|
||||
end
|
||||
alias_method :eql?, :==
|
||||
|
||||
def respond_to?(key, include_private=false)
|
||||
key?(key) or @default.respond_to?(key) or super(key, include_private)
|
||||
end
|
||||
|
@ -100,12 +101,6 @@ module Pry::Config::Behavior
|
|||
end
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
return false unless other.respond_to?(:to_hash)
|
||||
to_hash == other.to_hash
|
||||
end
|
||||
alias_method :eql?, :==
|
||||
|
||||
def keys
|
||||
@lookup.keys
|
||||
end
|
||||
|
@ -123,4 +118,16 @@ private
|
|||
value.dup
|
||||
end
|
||||
end
|
||||
|
||||
def try_convert_to_hash(obj)
|
||||
if Hash === obj
|
||||
obj
|
||||
elsif obj.respond_to?(:to_h)
|
||||
obj.to_h
|
||||
elsif obj.respond_to?(:to_hash)
|
||||
obj.to_hash
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue