提交 7b92798d 编写于 作者: S Sean Griffin

Merge pull request #21302 from theunraveler/delegate_reserved_argument_names

ActiveSupport: Fixing issue when delegating to methods named "block", "args", or "arg"
......@@ -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 @@ def delegate(*methods)
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 []=
......
......@@ -83,6 +83,16 @@ def type
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 @@ def test_delegation_doesnt_mask_nested_no_method_error_on_nil_receiver
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册