Project

General

Profile

Actions

Bug #3352

closed

Delegates: protected methods

Added by marcandre (Marc-Andre Lafortune) almost 14 years ago. Updated almost 13 years ago.

Status:
Rejected
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
Actions #1

Updated by mame (Yusuke Endoh) almost 14 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
=end

Actions #2

Updated by mame (Yusuke Endoh) almost 14 years ago

=begin
Hi,

2010/5/30 Marc-Andre Lafortune :

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

=end

Actions

Also available in: Atom PDF

Like0
Like0Like0