Is it possible to get all the eigenclasses in Ruby?
Asked Answered
C

3

13

Getting a list of all modules is easy in Ruby:

ObjectSpace.each_object(Module).to_a

However, is it possible to get a list of all eigenclasses (also known as singleton classes or metaclasses)? Or are eigenclasses invisible?

I tried

str = "foo"
my_metaclass = class << str; self; end
my_metaclass.class == Class # my_metaclass' class is Class
ObjectSpace.each_object(Class).include?(my_metaclass) # false
ObjectSpace.each_object.include?(my_metaclass) # still false
# Just to show each_object works
ObjectSpace.each_object(Class).include?(String) # true

I'm trying to get eigenclasses because I'm wanting to list all the methods that are defined within a script. I could look for all the instance methods defined by modules and classes, and then look for singleton methods of modules and classes (or of all objects, if I want to chew up CPU), but that seems a little hackish.

Camilla answered 19/10, 2011 at 3:50 Comment(7)
Did you end up finding an answer to this? nice question ;)Sirotek
I couldn't find a way to do this without calling each_object on each class. One thing I did notice though was that ObjectSpace.count_objects() :T_CLASS count increments with each eigenclass created. So it may be possible to do this in C land?Chesty
@DanHealy: What do you mean by each_object on each class? As in classes = ObjectSpace.each_object(Class).to_a; objects = classes.map{|klass| ObjectSpace.each_object(klass).to_a}.flatten?Camilla
I was basically referring to the method you suggested in your question: iterating over all objects.Chesty
woohoo! I love this question.Bursary
You must be doing something seriously meta. =PUngava
@thomasfedb: I'm working on something where, if there's a method_missing that doesn't get handled, it says something like ":foo isn't implemented in YourFirstClass, but it's implemented in YourOtherClass". I need to put it on github soon.Camilla
A
2

If you mean objects that have singleton methods, this should work.

eigens = []
ObjectSpace.each_object do |object|
  eigens << object unless object.singleton_methods.empty?
end

If not, could you clarify? I used this discussion as a reference:

http://www.ruby-forum.com/topic/77046

Agglutination answered 6/11, 2011 at 14:59 Comment(2)
The main disadvantage of this approach is that iterating through each object seems inefficient.Camilla
It is. :-) Is this something you need to do frequently in your program? I'm keen to see what you're up to, if you're able to share.Agglutination
S
1

I doubt this is what you want, but it should return all eigenclasses:

eigens = ObjectSpace.each_object.collect { |obj| class << obj; self; end }

That will indeed assign an array of all the eigenclasses to the variable eigens. The thing is, Ruby implementations likely don't actually create an eigenclass unless there is a need for it, and this code (I believe) will actually create the eigen classes even for the objects where one wasn't needed.

If finding a better way is important, I'd tweet the question to one of the implementors of any of the Ruby implementations (@yukihiro_matz, @evanphx, @headius to name a few that come to mind). If anybody would know, they would.

Sooty answered 25/10, 2011 at 23:21 Comment(0)
F
1

As of MRI 1.9, eigenclass enumeration does NOT seem to be supported. As a (semi-)consequence, there is no 100%-reliable way to iterate over all methods. The best approximation for an overall method enumerator is as follows

methods = []

ObjectSpace.each_object { |x|
  if x.kind_of?(Module)
    methods += x.public_instance_methods(false) +
               x.protected_instance_methods(false) +
               x.private_instance_methods(false)
  end
  methods +=   x.singleton_methods(false)
}

However, this code will NOT enumerate

  • private methods owned by 1st eigenclasses,
  • methods owned by 2nd, 3rd, ... eigenclasses.
Flection answered 14/12, 2011 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.