Lance Pollard
Lance Pollard

Reputation: 79228

Method to tell if ancestor is a Class or Module in Ruby?

If I have a class like the following, how do I tell when an ancestor is a class vs. a module?

ActiveRecord::Base.send(:include, SomeLibrary)

class Group < ActiveRecord::Base
  include SomeLibrary::Core
end

class SubGroup < Group

end

ancestor_names = SubGroup.ancestors.map(&:name)
puts ancestor_names.inspect
  #=> [
  "SubGroup", "SomeLibrary::Core::InstanceMethods", "SomeLibrary::Core", 
  "Group", "ActiveRecord::Base", "SomeLibrary", "ActiveRecord::Aggregations", 
  "ActiveRecord::Transactions", "ActiveRecord::Reflection", "ActiveRecord::Batches", 
  "ActiveRecord::Calculations", "ActiveRecord::Serialization", "ActiveRecord::AutosaveAssociation", 
  "ActiveRecord::NestedAttributes", "ActiveRecord::Associations", "ActiveRecord::AssociationPreload", 
  "ActiveRecord::NamedScope", "ActiveRecord::Callbacks", "ActiveRecord::Observing", 
  "ActiveRecord::Timestamp", "ActiveRecord::Dirty", "ActiveRecord::AttributeMethods", 
  "ActiveRecord::Locking::Optimistic", "ActiveRecord::Locking::Pessimistic", 
  "ActiveSupport::Callbacks", "ActiveRecord::Validations", "Object", "Mocha::ObjectMethods", 
  "JSON::Pure::Generator::GeneratorMethods::Object", "ActiveSupport::Dependencies::Loadable", 
  "Base64::Deprecated", "Base64", "PP::ObjectMixin", "Kernel"
]

I would like to be able to do something like this:

ancestor_names.each do |name|
  if class?(name)
    puts "#{name} is a Class"
  elsif module?(name)
    puts "#{name} is a Module"
  end
end

or...

SubGroup.ancestor_classes #=> only the classes in the tree
SubGroup.ancestor_modules #=> only the modules in the tree

It boils down to, how do you check if an object is a class or module?

Upvotes: 14

Views: 5092

Answers (3)

Jak
Jak

Reputation: 3233

Above answers are good enough. This below also could be helpful.

pp SubGroup.ancestors.map {|c| c.instance_of?(Class) ? "CLASS: #{c}" : "MODULE: #{c}" }

Upvotes: 0

molf
molf

Reputation: 74945

Well, that is what included_modules is for, so what you're probably looking for is:

SubGroup.included_modules                       #=> module ancestors
SubGroup.ancestors - SubGroup.included_modules  #=> class ancestors

Or, with a helper method:

class Class
  def ancestor_classes
    ancestors - included_modules
  end
end

SubGroup.included_modules  #=> module ancestors
SubGroup.ancestor_classes  #=> class ancestors

Upvotes: 15

J&#246;rg W Mittag
J&#246;rg W Mittag

Reputation: 369438

Use Object#instance_of?:

Object.instance_of? Class  # => true
Object.instance_of? Module # => false
Kernel.instance_of? Class  # => false
Kernel.instance_of? Module # => true

Upvotes: 13

Related Questions