Skip to content

Instantly share code, notes, and snippets.

@tobiashm
Created September 20, 2013 11:06
Show Gist options
  • Save tobiashm/6636001 to your computer and use it in GitHub Desktop.
Save tobiashm/6636001 to your computer and use it in GitHub Desktop.
Example of how to determine if a class method is defined on the current class or an ancestor. Notice that Method#owner returns the metaclass, so we can't just compare to the class itself.
base_classes_with_own_connection = ActiveRecord::Base.subclasses.find_all { |klass|
klass.method(:establish_connection).owner == (class << klass; self; end)
}
([ActiveRecord::Base] + base_classes_with_own_connection).each do |base|
base.establish_connection
end
@tobiashm
Copy link
Author

Alternative predicate:

(class << klass; self; end).instance_methods(false).include?(:establish_connection)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment