Bug #2496
closedDelegate: #methods and #public_methods should return delegated methods too
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
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 mame@tsg.ne.jp
=end
Updated by mame (Yusuke Endoh) over 14 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
Updated by marcandre (Marc-Andre Lafortune) over 14 years ago
- Assignee changed from yugui (Yuki Sonoda) to marcandre (Marc-Andre Lafortune)
=begin
=end
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