summaryrefslogtreecommitdiff
path: root/lib/forwardable.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/forwardable.rb')
-rw-r--r--lib/forwardable.rb52
1 files changed, 27 insertions, 25 deletions
diff --git a/lib/forwardable.rb b/lib/forwardable.rb
index e8333a359e..175d6d9c6b 100644
--- a/lib/forwardable.rb
+++ b/lib/forwardable.rb
@@ -76,7 +76,7 @@
# def_delegators :@q, :clear, :first, :push, :shift, :size
# end
#
-# q = Queue.new
+# q = Thread::Queue.new
# q.enq 1, 2, 3, 4, 5
# q.push 6
#
@@ -109,8 +109,13 @@
# +delegate.rb+.
#
module Forwardable
- require 'forwardable/impl'
- require "forwardable/version"
+ # Version of +forwardable.rb+
+ VERSION = "1.4.0"
+ VERSION.freeze
+
+ # Version for backward compatibility
+ FORWARDABLE_VERSION = VERSION
+ FORWARDABLE_VERSION.freeze
@debug = nil
class << self
@@ -160,6 +165,7 @@ module Forwardable
# +accessor.method+. +accessor+ should be a method name, instance
# variable name, or constant name. Use the full path to the
# constant if providing the constant name.
+ # Returns the name of the method defined.
#
# class MyQueue
# CONST = 1
@@ -185,7 +191,6 @@ module Forwardable
# If it's not a class or module, it's an instance
mod = Module === self ? self : singleton_class
mod.module_eval(&gen)
- mod.send(:ruby2_keywords, ali) if RUBY_VERSION >= '2.7'
end
alias delegate instance_delegate
@@ -199,36 +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
- method_call = ".__send__(:#{method}, *args, &block)"
- if _valid_method?(method)
+ args = RUBY_VERSION >= '2.7' ? '...' : '*args, &block'
+ method_call = ".__send__(:#{method}, #{args})"
+ 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, &block)
- 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, &block)
- #{pre}
- begin
- #{accessor}
- end#{method_call}
+ def #{ali}(#{args})
+ #{pre}#{accessor}
+ #{method_call}
end
end
- end;
+ RUBY
end
end
@@ -299,11 +301,11 @@ module SingleForwardable
# Defines a method _method_ which delegates to _accessor_ (i.e. it calls
# the method of the same name in _accessor_). If _new_name_ is
# provided, it is used as the name for the delegate method.
+ # Returns the name of the method defined.
def def_single_delegator(accessor, method, ali = method)
gen = Forwardable._delegator_method(self, accessor, method, ali)
instance_eval(&gen)
- singleton_class.send(:ruby2_keywords, ali) if RUBY_VERSION >= '2.7'
end
alias delegate single_delegate