Project

General

Profile

Actions

Bug #2496

closed

Delegate: #methods and #public_methods should return delegated methods too

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

Status:
Closed
Target version:
ruby -v:
ruby 1.9.2dev (2009-12-19 trunk 26121) [x86_64-darwin10.2.0]
Backport:
[ruby-core:27224]

Description

=begin
require 'delegate'
s = SimpleDelegator.new "Hello, world!"
s.respond_to? :upcase # => true
s.method :upcase # => #<Method : SimpleDelegator#upcase>
s.methods.include? :upcase # => false, true expected

Similar problem with public_methods.
I propose that they return the union of methods from the delegator object and the methods of the object delegated to (only the public ones, since other ones are not forwarded)

Patch:
diff --git a/lib/delegate.rb b/lib/delegate.rb
index 77804e4..2fd5b49 100644
--- a/lib/delegate.rb
+++ b/lib/delegate.rb
@@ -158,6 +158,22 @@ class Delegator
end

#
  • Returns the methods available to this delegate object as the union

  • of this object's methods and the public methods of __getobj__.

  • def methods
  • self.getobj.public_methods | super
  • end
  • Returns the methods available to this delegate object as the union of this object

  • and the methods of __getobj__.

  • def public_methods(all=true)
  • self.getobj.public_methods(all) | super
  • end
  • Returns true if two objects are considered same.

    def ==(obj)
    =end
Actions #1

Updated by mame (Yusuke Endoh) over 14 years ago

  • Assignee set to yugui (Yuki Sonoda)

=begin
Hi,

Agreed.
The patch is slightly wrong; methods should call getobj.methods?

Matz or Yugui, do you allow to import this patch?

--
Yusuke Endoh
=end

Actions #2

Updated by mame (Yusuke Endoh) over 14 years ago

=begin
Hi,

2010/4/28 Marc-Andre Lafortune :

Hi,

On Wed, Apr 28, 2010 at 8:49 AM, Yusuke Endoh wrote:

The patch is slightly wrong; methods should call getobj.methods?

The reason I've proposed public_methods is that delegates do not
forward private methods. I feel it would be best if the following is
true for any object, including delegates:

   obj.methods.all?{|m| obj.respond_to?(m, true)}

Ah, yes, I remember. Thanks.

--
Yusuke Endoh

=end

Actions #3

Updated by marcandre (Marc-Andre Lafortune) over 14 years ago

  • Assignee changed from yugui (Yuki Sonoda) to marcandre (Marc-Andre Lafortune)

=begin

=end

Actions #4

Updated by marcandre (Marc-Andre Lafortune) over 14 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

=begin
This issue was solved with changeset r28099.
Marc-Andre, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

=end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0