From 756da742b14837f631ad124b4f4b7ffa58af8088 Mon Sep 17 00:00:00 2001 From: Soutaro Matsumoto Date: Thu, 24 Oct 2019 16:59:25 +0900 Subject: [PATCH 1/3] Stop using `self` methods directly --- lib/ruby/signature/test/hook.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/ruby/signature/test/hook.rb b/lib/ruby/signature/test/hook.rb index 971b22cb9..d77d47f69 100644 --- a/lib/ruby/signature/test/hook.rb +++ b/lib/ruby/signature/test/hook.rb @@ -4,11 +4,13 @@ module Ruby module Signature module Test class Hook - IS_AP = Object.instance_method(:is_a?) + IS_AP = Kernel.instance_method(:is_a?) DEFINE_METHOD = Module.instance_method(:define_method) INSTANCE_EVAL = BasicObject.instance_method(:instance_eval) INSTANCE_EXEC = BasicObject.instance_method(:instance_exec) METHOD = Kernel.instance_method(:method) + CLASS = Kernel.instance_method(:class) + SINGLETON_CLASS = Kernel.instance_method(:singleton_class) module Errors ArgumentTypeError = @@ -187,8 +189,8 @@ def delegation(name, method_types, method_name) end end - method = self.method(name) - prepended = self.class.ancestors.include?(hook.instance_module) || self.singleton_class.ancestors.include?(hook.singleton_module) + method = hook.call(self, METHOD, name) + prepended = hook.call(self, CLASS).ancestors.include?(hook.instance_module) || hook.call(self, SINGLETON_CLASS).ancestors.include?(hook.singleton_module) result = if prepended method.super_method.call(*args, &block) else From 2a47654e84c0e06e67fe6f2c0b6964b498d167ec Mon Sep 17 00:00:00 2001 From: Soutaro Matsumoto Date: Thu, 24 Oct 2019 23:10:23 +0900 Subject: [PATCH 2/3] Add PP for debugging --- lib/ruby/signature/test/hook.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/ruby/signature/test/hook.rb b/lib/ruby/signature/test/hook.rb index d77d47f69..02aec4271 100644 --- a/lib/ruby/signature/test/hook.rb +++ b/lib/ruby/signature/test/hook.rb @@ -1,4 +1,5 @@ require "ruby/signature" +require "pp" module Ruby module Signature @@ -11,6 +12,7 @@ class Hook METHOD = Kernel.instance_method(:method) CLASS = Kernel.instance_method(:class) SINGLETON_CLASS = Kernel.instance_method(:singleton_class) + PP = Kernel.instance_method(:pp) module Errors ArgumentTypeError = From c3a6ae8b08831438220afc103974d9b1560c5357 Mon Sep 17 00:00:00 2001 From: Soutaro Matsumoto Date: Fri, 25 Oct 2019 15:32:58 +0900 Subject: [PATCH 3/3] Functionalize inspect --- lib/ruby/signature/test/hook.rb | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/ruby/signature/test/hook.rb b/lib/ruby/signature/test/hook.rb index 02aec4271..ae2ecdfe0 100644 --- a/lib/ruby/signature/test/hook.rb +++ b/lib/ruby/signature/test/hook.rb @@ -13,6 +13,7 @@ class Hook CLASS = Kernel.instance_method(:class) SINGLETON_CLASS = Kernel.instance_method(:singleton_class) PP = Kernel.instance_method(:pp) + INSPECT = Kernel.instance_method(:inspect) module Errors ArgumentTypeError = @@ -41,21 +42,25 @@ def self.format_param(param) end end + def self.inspect_(obj) + Hook.inspect_(obj) + end + def self.to_string(error) method = "#{error.klass.name}#{error.method_name}" case error when ArgumentTypeError - "[#{method}] ArgumentTypeError: expected #{format_param error.param} but given `#{error.value.inspect}`" + "[#{method}] ArgumentTypeError: expected #{format_param error.param} but given `#{inspect_(error.value)}`" when BlockArgumentTypeError - "[#{method}] BlockArgumentTypeError: expected #{format_param error.param} but given `#{error.value.inspect}`" + "[#{method}] BlockArgumentTypeError: expected #{format_param error.param} but given `#{inspect_(error.value)}`" when ArgumentError "[#{method}] ArgumentError: expected method type #{error.method_type}" when BlockArgumentError "[#{method}] BlockArgumentError: expected method type #{error.method_type}" when ReturnTypeError - "[#{method}] ReturnTypeError: expected `#{error.type}` but returns `#{error.value.inspect}`" + "[#{method}] ReturnTypeError: expected `#{error.type}` but returns `#{inspect_(error.value)}`" when BlockReturnTypeError - "[#{method}] BlockReturnTypeError: expected `#{error.type}` but returns `#{error.value.inspect}`" + "[#{method}] BlockReturnTypeError: expected `#{error.type}` but returns `#{inspect_(error.value)}`" when UnexpectedBlockError "[#{method}] UnexpectedBlockError: unexpected block is given for `#{error.method_type}`" when MissingBlockError @@ -63,7 +68,7 @@ def self.to_string(error) when UnresolvedOverloadingError "[#{method}] UnresolvedOverloadingError: couldn't find a suitable overloading" else - raise "Unexpected error: #{error.inspect}" + raise "Unexpected error: #{inspect_(error)}" end end end @@ -165,7 +170,7 @@ def delegation(name, method_types, method_name) hook = self proc do |*args, &block| - hook.logger.debug { "#{method_name} receives arguments: #{args.inspect}" } + hook.logger.debug { "#{method_name} receives arguments: #{hook.inspect_(args)}" } block_call = nil @@ -174,7 +179,7 @@ def delegation(name, method_types, method_name) block = hook.call(Object.new, INSTANCE_EVAL) do |fresh_obj| proc do |*as| - hook.logger.debug { "#{method_name} receives block arguments: #{as.inspect}" } + hook.logger.debug { "#{method_name} receives block arguments: #{hook.inspect_(as)}" } ret = if self.equal?(fresh_obj) original_block[*as] @@ -184,7 +189,7 @@ def delegation(name, method_types, method_name) block_call = ArgsReturn.new(arguments: as, return_value: ret) - hook.logger.debug { "#{method_name} returns from block: #{ret.inspect}" } + hook.logger.debug { "#{method_name} returns from block: #{hook.inspect_(ret)}" } ret end @@ -200,7 +205,7 @@ def delegation(name, method_types, method_name) method.call(*args, &block) end - hook.logger.debug { "#{method_name} returns: #{result.inspect}" } + hook.logger.debug { "#{method_name} returns: #{hook.inspect_(result)}" } call = Call.new(method_call: ArgsReturn.new(arguments: args, return_value: result), block_call: block_call, @@ -314,6 +319,14 @@ def call(receiver, method, *args, &block) method.bind(receiver).call(*args, &block) end + def inspect_(obj) + Hook.inspect_(obj) + end + + def self.inspect_(obj) + INSPECT.bind(obj).call() + end + def disable self.instance_module.remove_method(*instance_methods) self.singleton_module.remove_method(*singleton_methods)