1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/lib
nobu 986fb55961 Forwardable: Fix delegating to 'args' and 'block'
* lib/forwardable.rb (def_instance_delegator) fix delegating to
  'args' and 'block', clashing with local variables in generated
  methods.  [ruby-core:72579] [Bug #11916]

* lib/forwardable.rb (def_single_delegator): ditto.

If you have a class that uses Forwardable to delegate a method to
another object, and the method that returns the delegate object is
called `args` or `block`, then Forwardable will fail to work.

Here's a simple example:

    class ModelCreator
      extend Forwardable

      attr_reader :args

      def_delegator :args, :model_name

      def initialize(args)
        @args = args
      end
    end

    ModelCreator.new.model_name

If you run the last line above, then you'll get:

    NoMethodError: undefined method `model_name' for []:Array

This error occurs because `def_delegator` -- as it is written in Ruby --
uses metaprogramming to add methods to the class that will then delegate
to the delegate object. So it's as if we had written:

    class ModelCreator
      extend Forwardable

      attr_reader :args

      def model_name(*args, &block)
        args.model_name(*args, &block)
      end

      def initialize(args)
        @args = args
      end
    end

As you can see, `def_delegator` will not only forward the method call
onto the delegate object, it will also forward any arguments provided as
well. It is here that the bug arises: it splats all of the arguments
into a variable which is called `args`, and because of how variable
scope works in Ruby, it then attempts to call `model_name` on *this*
variable and *not* our delegate object method.

The fix is to call the delegate object method manually using `__send__`.
(This assumes, of course, that the given receiver is, in fact, the name
of a method and not the name of an instance variable, which is also a
possibility.) We use `__send__` because the delegate object method could
be private.

So, that looks like this:

    def model_name(*args, &block)
      __send__(:args).model_name(*args, &block)
    end

Because `def_delegators` and `delegate` use `def_delegator` internally,
they also get this fix as well.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53381 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-12-30 02:18:44 +00:00
..
cgi cgi/escape: Optimize CGI.escapeHTML 2015-12-20 11:54:54 +00:00
drb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
irb * lib/irb/ruby-lex.rb: fixed parse error for striped heredocument syntax. 2015-12-18 01:25:08 +00:00
matrix Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
net [DOC] Fix typos 2015-12-23 03:43:23 +00:00
optparse Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
racc Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
rbconfig Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
rdoc * lib/rdoc.rb: bump version to 4.2.1. It contains following fixes. 2015-12-22 12:08:13 +00:00
rexml Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
rinda Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
rss Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
rubygems Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
shell Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
unicode_normalize Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
uri Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
webrick webrick/utils.rb: get rid of thread leak checker 2015-12-19 08:16:54 +00:00
xmlrpc * lib/xmlrpc/client.rb: Support SSL options in async methods of 2015-12-26 09:33:43 +00:00
yaml Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
abbrev.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
base64.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
benchmark.rb [DOC] Fix typos 2015-12-23 03:43:23 +00:00
cgi.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
cmath.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
csv.rb csv.rb: Fix typo [ci skip 2015-12-19 05:23:50 +00:00
debug.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
delegate.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
drb.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
e2mmap.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
English.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
erb.rb * lib/erb.rb: revert r53123. It break compatibility like thor and rspec-rails. 2015-12-20 06:36:57 +00:00
fileutils.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
find.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
forwardable.rb Forwardable: Fix delegating to 'args' and 'block' 2015-12-30 02:18:44 +00:00
getoptlong.rb [DOC] Fix typos 2015-12-23 03:43:23 +00:00
ipaddr.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
irb.rb [DOC] Fix typos 2015-12-23 03:43:23 +00:00
logger.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
mathn.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
matrix.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
mkmf.rb remove duplicated frozen_string_literal magic comment 2015-12-16 09:25:48 +00:00
monitor.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
mutex_m.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
observer.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
open-uri.rb * lib/net/ftp.rb (initialize): Connections are in passive mode per 2015-11-11 03:48:45 +00:00
open3.rb * lib/open3.rb: Specify frozen_string_literal: true. 2015-11-14 07:43:23 +00:00
optionparser.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
optparse.rb [DOC] Fix typos 2015-12-23 03:43:23 +00:00
ostruct.rb ostruct.rb: respond_to? 2015-12-29 03:48:36 +00:00
pp.rb * lib/open-uri.rb: Remove indicator for "frozen_string_literal: true". 2015-11-10 11:48:14 +00:00
prettyprint.rb * lib/open-uri.rb: Remove indicator for "frozen_string_literal: true". 2015-11-10 11:48:14 +00:00
prime.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
profile.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
profiler.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
pstore.rb [DOC] Fix typos 2015-12-23 03:43:23 +00:00
rdoc.rb * lib/rdoc.rb: bump version to 4.2.1. It contains following fixes. 2015-12-22 12:08:13 +00:00
resolv-replace.rb * lib/resolv-replace.rb: Specify frozen_string_literal: true. 2015-11-14 08:25:30 +00:00
resolv.rb Resolv::IPv6.create: avoid modifying frozen string literal 2015-12-28 20:31:10 +00:00
rss.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
rubygems.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
scanf.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
securerandom.rb * lib/securerandom.rb (SecureRandom::gen_random): use /dev/urandom 2015-11-30 20:29:22 +00:00
set.rb * lib/set.rb: Enable frozen_string_literal. 2015-11-16 07:41:30 +00:00
shell.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
shellwords.rb remove duplicated frozen_string_literal magic comment 2015-12-16 09:25:48 +00:00
singleton.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
sync.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
tempfile.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
thwait.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
time.rb * lib/time.rb: Use "<<" to reduce string allocation. 2015-11-14 12:45:30 +00:00
timeout.rb timeout.rb: watcher thread name 2015-12-18 15:46:50 +00:00
tmpdir.rb * lib/open-uri.rb: Remove indicator for "frozen_string_literal: true". 2015-11-10 11:48:14 +00:00
tracer.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
tsort.rb * lib/tsort.rb: Specify frozen_string_literal: true. 2015-11-14 08:46:11 +00:00
ubygems.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
un.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
unicode_normalize.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
uri.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
weakref.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
webrick.rb [DOC] Fix typos 2015-12-23 03:43:23 +00:00
xmlrpc.rb [DOC] Fix typos 2015-12-23 03:43:23 +00:00
yaml.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00