diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2015-10-20 15:04:05 -0600 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2015-10-20 15:04:05 -0600 |
commit | 7b92798d2fee012bf683c513fb3864a9143a6f71 (patch) | |
tree | 85573c9f8661cddb06011da1b76517ebf8f1d175 | |
parent | 3e29d96ecda48a287601ec09321f7444df8266be (diff) | |
parent | 47ab9a04d3317d7d42c7b2e5709b8e861422ebc3 (diff) | |
download | rails-7b92798d2fee012bf683c513fb3864a9143a6f71.tar.gz rails-7b92798d2fee012bf683c513fb3864a9143a6f71.tar.bz2 rails-7b92798d2fee012bf683c513fb3864a9143a6f71.zip |
Merge pull request #21302 from theunraveler/delegate_reserved_argument_names
ActiveSupport: Fixing issue when delegating to methods named "block", "args", or "arg"
-rw-r--r-- | activesupport/lib/active_support/core_ext/module/delegation.rb | 11 | ||||
-rw-r--r-- | activesupport/test/core_ext/module_test.rb | 15 |
2 files changed, 21 insertions, 5 deletions
diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index 9dc0dee1bd..0d46248582 100644 --- a/activesupport/lib/active_support/core_ext/module/delegation.rb +++ b/activesupport/lib/active_support/core_ext/module/delegation.rb @@ -5,10 +5,11 @@ class Module # option is not used. class DelegationError < NoMethodError; end - RUBY_RESERVED_WORDS = Set.new( - %w(alias and BEGIN begin break case class def defined? do else elsif END - end ensure false for if in module next nil not or redo rescue retry - return self super then true undef unless until when while yield) + DELEGATION_RESERVED_METHOD_NAMES = Set.new( + %w(_ arg args alias and BEGIN begin block break case class def defined? do + else elsif END end ensure false for if in module next nil not or redo + rescue retry return self super then true undef unless until when while + yield) ).freeze # Provides a +delegate+ class method to easily expose contained objects' @@ -171,7 +172,7 @@ class Module line = line.to_i to = to.to_s - to = "self.#{to}" if RUBY_RESERVED_WORDS.include?(to) + to = "self.#{to}" if DELEGATION_RESERVED_METHOD_NAMES.include?(to) methods.each do |method| # Attribute writer methods only accept one argument. Makes sure []= diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index bdfbadcf1d..0ed66f8c37 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -83,6 +83,16 @@ Product = Struct.new(:name) do end end +class Block + def hello? + true + end +end + +HasBlock = Struct.new(:block) do + delegate :hello?, to: :block +end + class ParameterSet delegate :[], :[]=, :to => :@params @@ -301,6 +311,11 @@ class ModuleTest < ActiveSupport::TestCase assert_raise(NoMethodError) { product.type_name } end + def test_delegation_with_method_arguments + has_block = HasBlock.new(Block.new) + assert has_block.hello? + end + def test_parent assert_equal Yz::Zy, Yz::Zy::Cd.parent assert_equal Yz, Yz::Zy.parent |