[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

about singleton_methods

Kyung won Cheon

1/8/2009 7:30:00 AM

class A
class << A
def aaa
end

protected
def bbb
end

private
def ccc
end
end
end

p A.singleton_methods # => ["aaa", "bbb"]

# private method is not a singleton method.. Why?
# Anyway 'ccc' is defined in a singleton class of A ?

###############
# Help Me^^
###############
--
Posted via http://www.ruby-....

1 Answer

Robert Dober

1/8/2009 9:06:00 AM

0

On Thu, Jan 8, 2009 at 8:29 AM, Kyung won Cheon <kdream95@gmerce.co.kr> wrote:
> class A
> class << A
> def aaa
> end
>
> protected
> def bbb
> end
>
> private
> def ccc
> end
> end
> end
>
> p A.singleton_methods # => ["aaa", "bbb"]
>
> # private method is not a singleton method.. Why?
> # Anyway 'ccc' is defined in a singleton class of A ?
>
> ###############
> # Help Me^^
> ###############
> --
> Posted via http://www.ruby-....
>
>
I have never noticed that before, I certainly do not like that.

If you need that information you could do the following

class Object
def my_sing_methods # this does not sing
singleton_methods + begin
class << self; self end.private_instance_methods( false )
rescue
[ ]
end
end
end

This however gives you three more methods, namely :inherited,
:initialize, :initialize_copy.
You can of course get rid of them, but what if they are overloaded?
Unfortunately I can not
think of any simple code to cover that.

Cheers
Robert

--
It is change, continuing change, inevitable change, that is the
dominant factor in society today. No sensible decision can be made any
longer without taking into account not only the world as it is, but
the world as it will be ... ~ Isaac Asimov