about singleton_methods

  • Thread starter Kyung won Cheon
  • Start date
K

Kyung won Cheon

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^^
###############
 
R

Robert Dober

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^^
###############
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top