From ce7dcf847331c4bd2d2216f3168b9e1a5201bd24 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 23 Mar 2014 09:57:59 +0100 Subject: [PATCH] support #to_h, #to_hash in Behavior#== --- lib/pry/config/behavior.rb | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 2480e0be..9a8c0dd3 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -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