Skip to content

Commit

Permalink
Merge pull request #21302 from theunraveler/delegate_reserved_argumen…
Browse files Browse the repository at this point in the history
…t_names

ActiveSupport: Fixing issue when delegating to methods named "block", "args", or "arg"
  • Loading branch information
sgrif committed Oct 20, 2015
2 parents 3e29d96 + 47ab9a0 commit 7b92798
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
11 changes: 6 additions & 5 deletions activesupport/lib/active_support/core_ext/module/delegation.rb
Expand Up @@ -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'
Expand Down Expand Up @@ -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 []=
Expand Down
15 changes: 15 additions & 0 deletions activesupport/test/core_ext/module_test.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7b92798

Please sign in to comment.