diff --git a/lib/pry.rb b/lib/pry.rb index fd33728e..7ccded7a 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -2,12 +2,11 @@ # MIT License # require 'pp' - +require 'pry/forwardable' require 'pry/input_lock' require 'pry/exceptions' require 'pry/helpers/base_helpers' require 'pry/hooks' -require 'forwardable' class Pry # The default hooks - display messages when beginning and ending Pry sessions. diff --git a/lib/pry/forwardable.rb b/lib/pry/forwardable.rb new file mode 100644 index 00000000..71cafb9f --- /dev/null +++ b/lib/pry/forwardable.rb @@ -0,0 +1,23 @@ +class Pry + module Forwardable + require 'forwardable' + include ::Forwardable + + # + # Since Ruby 2.4, Forwardable will print a warning when + # calling a method that is private on a delegate, and + # in the future it could be an error: https://bugs.ruby-lang.org/issues/12782#note-3 + # + # That's why we revert to a custom implementation for delegating one + # private method to another. + # + def def_private_delegators(target, *private_delegates) + private_delegates.each do |private_delegate| + define_method(private_delegate) do |*a, &b| + instance_variable_get(target).__send__(private_delegate, *a, &b) + end + end + class_eval { private *private_delegates } + end + end +end diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 1609f376..95a574f9 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -5,7 +5,7 @@ class Pry LOCAL_RC_FILE = "./.pryrc" class << self - extend Forwardable + extend Pry::Forwardable attr_accessor :custom_completions attr_accessor :current_line attr_accessor :line_buffer diff --git a/lib/pry/repl.rb b/lib/pry/repl.rb index dba4bf5a..b94159d7 100644 --- a/lib/pry/repl.rb +++ b/lib/pry/repl.rb @@ -2,7 +2,7 @@ require 'forwardable' class Pry class REPL - extend Forwardable + extend Pry::Forwardable def_delegators :@pry, :input, :output # @return [Pry] The instance of {Pry} that the user is controlling. diff --git a/lib/pry/test/helper.rb b/lib/pry/test/helper.rb index 2ef8289b..e71536a0 100644 --- a/lib/pry/test/helper.rb +++ b/lib/pry/test/helper.rb @@ -102,7 +102,7 @@ def pry_eval(*eval_strs) end class PryTester - extend Forwardable + extend Pry::Forwardable attr_reader :pry, :out diff --git a/lib/pry/wrapped_module/candidate.rb b/lib/pry/wrapped_module/candidate.rb index e35c0356..67ef1925 100644 --- a/lib/pry/wrapped_module/candidate.rb +++ b/lib/pry/wrapped_module/candidate.rb @@ -1,5 +1,4 @@ require 'pry/helpers/documentation_helpers' -require 'forwardable' class Pry class WrappedModule @@ -10,7 +9,7 @@ class Pry class Candidate include Pry::Helpers::DocumentationHelpers include Pry::CodeObject::Helpers - extend Forwardable + extend Pry::Forwardable # @return [String] The file where the module definition is located. attr_reader :file @@ -22,15 +21,12 @@ class Pry # Methods to delegate to associated `Pry::WrappedModule # instance`. - private_delegates = [:lines_for_file, :method_candidates, - :yard_docs?] - - public_delegates = [:wrapped, :module?, :class?, :name, :nonblank_name, + private_delegates = [:lines_for_file, :method_candidates, :yard_docs?, :name] + public_delegates = [:wrapped, :module?, :class?, :nonblank_name, :number_of_candidates] - def_delegators :@wrapper, *(private_delegates + public_delegates) - private(*private_delegates) - public(*public_delegates) + def_delegators :@wrapper, *public_delegates + def_private_delegators :@wrapper, *private_delegates # @raise [Pry::CommandError] If `rank` is out of bounds. # @param [Pry::WrappedModule] wrapper The associated