summaryrefslogtreecommitdiff
path: root/lib/forwardable.rb
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2025-11-24 08:48:19 +0100
committergit <svn-admin@ruby-lang.org>2025-12-10 07:05:06 +0000
commit14ff851185bb8ff399e98b74cc107302a4e08e18 (patch)
treef81f5b8cad2890f79921085adec50fb8e07dd5ab /lib/forwardable.rb
parent375025a3864fc944dc9f42909a6c5386c749d5fd (diff)
[ruby/forwardable] Simpler and faster check for the delegation fastpath
Fix: https://github.com/ruby/forwardable/issues/35 [Bug #21708] Trying to compile code to check if a method can use the delegation fastpath is a bit wasteful and cause `RUPYOPT=-d` to be full of misleading errors. It's simpler and faster to use a simple regexp to do the same check. https://github.com/ruby/forwardable/commit/de1fbd182e
Diffstat (limited to 'lib/forwardable.rb')
-rw-r--r--lib/forwardable.rb34
1 files changed, 14 insertions, 20 deletions
diff --git a/lib/forwardable.rb b/lib/forwardable.rb
index 76267c2cd1..040f467b23 100644
--- a/lib/forwardable.rb
+++ b/lib/forwardable.rb
@@ -109,8 +109,6 @@
# +delegate.rb+.
#
module Forwardable
- require 'forwardable/impl'
-
# Version of +forwardable.rb+
VERSION = "1.3.3"
VERSION.freeze
@@ -206,37 +204,33 @@ module Forwardable
if Module === obj ?
obj.method_defined?(accessor) || obj.private_method_defined?(accessor) :
obj.respond_to?(accessor, true)
- accessor = "#{accessor}()"
+ accessor = "(#{accessor}())"
end
args = RUBY_VERSION >= '2.7' ? '...' : '*args, &block'
method_call = ".__send__(:#{method}, #{args})"
- if _valid_method?(method)
+ if method.match?(/\A[_a-zA-Z]\w*[?!]?\z/)
loc, = caller_locations(2,1)
pre = "_ ="
mesg = "#{Module === obj ? obj : obj.class}\##{ali} at #{loc.path}:#{loc.lineno} forwarding to private method "
- method_call = "#{<<-"begin;"}\n#{<<-"end;".chomp}"
- begin;
- unless defined? _.#{method}
- ::Kernel.warn #{mesg.dump}"\#{_.class}"'##{method}', uplevel: 1
- _#{method_call}
- else
- _.#{method}(#{args})
- end
- end;
+ method_call = <<~RUBY.chomp
+ if defined?(_.#{method})
+ _.#{method}(#{args})
+ else
+ ::Kernel.warn #{mesg.dump}"\#{_.class}"'##{method}', uplevel: 1
+ _#{method_call}
+ end
+ RUBY
end
- _compile_method("#{<<-"begin;"}\n#{<<-"end;"}", __FILE__, __LINE__+1)
- begin;
+ eval(<<~RUBY, nil, __FILE__, __LINE__ + 1)
proc do
def #{ali}(#{args})
- #{pre}
- begin
- #{accessor}
- end#{method_call}
+ #{pre}#{accessor}
+ #{method_call}
end
end
- end;
+ RUBY
end
end