Project

General

Profile

Bug #2496

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

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

Status:
Closed
Priority:
Normal
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 # => #
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

History

#1

Updated by mame (Yusuke Endoh) about 9 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 mame@tsg.ne.jp
=end

#2

Updated by mame (Yusuke Endoh) about 9 years ago

=begin
Hi,

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

Hi,

On Wed, Apr 28, 2010 at 8:49 AM, Yusuke Endoh redmine@ruby-lang.org 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 mame@tsg.ne.jp

=end

#3

Updated by marcandre (Marc-Andre Lafortune) about 9 years ago

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

=begin

=end

#4

Updated by marcandre (Marc-Andre Lafortune) almost 9 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

Also available in: Atom PDF