Project

General

Profile

Bug #3352

Delegates: protected methods

Added by marcandre (Marc-Andre Lafortune) over 9 years ago. Updated over 8 years ago.

Status:
Rejected
Priority:
Normal
Target version:
ruby -v:
trunk
Backport:
[ruby-core:30451]

Description

=begin
require 'delegate'

 class X
   protected
     def pro
       :foo
     end
 end
 obj = X.new

 obj.pro #=> NoMethodError: protected method `pro' called for #<X:0x000001008a2a68>
 SimpleDelegator.new(obj).pro #=> :foo

I feel it would be more sensible to raise a NoMethodError.

No test seem to be testing for protected access, nor does RubySpec.

Unless there is objection, I'll commit the following:

diff --git a/lib/delegate.rb b/lib/delegate.rb
index f366091..93fbc37 100644
--- a/lib/delegate.rb
+++ b/lib/delegate.rb
@@ -141,7 +141,7 @@ class Delegator < BasicObject
def method_missing(m, *args, &block)
target = self.getobj
begin

  • target.respond_to?(m) ? target.send(m, *args, &block) : super(m, *args, &block)
  • target.respond_to?(m) ? target.public_send(m, *args, &block) : super ensure $@.delete_if {|t| %r"\A#{Regexp.quote(FILE)}:#{LINE-2}:"o =~ t} if $@ end =end

History

#1

Updated by mame (Yusuke Endoh) over 9 years ago

  • Status changed from Open to Rejected

=begin
See [ruby-core:30453].

Currently, there is no way for callee to know whether the method is
called as function style (without receiver) or normal method style
(with receiver).
So the call must be succeeded conservatively.

And, Marc-Andre, please take a look at #2223.

--
Yusuke Endoh mame@tsg.ne.jp
=end

#2

Updated by mame (Yusuke Endoh) over 9 years ago

=begin
Hi,

2010/5/30 Marc-Andre Lafortune ruby-core-mailing-list@marc-andre.ca:

And, Marc-Andre, please take a look at #2223.

Yes. This came up while working on the specs for that issue. Which
also means you were right that my patch there should not use
#public_methods

Aha! All right.

Last question: should the delegate object's protected method show up
in protected_methods (as was "intended"), or in public_methods (as is
in effect)?

I'd lean on listing it in protected_methods

I agree with you.

--
Yusuke Endoh mame@tsg.ne.jp

=end

Also available in: Atom PDF