Namespacing vs. subclassing

From WhyNotWiki

Jump to: navigation, search

Namespacing vs. subclassing  edit   (Category  edit)



[edit] In the context of Ruby

Also known as: "Ancestry vs. superclass", "Ancestry vs. instances of", "Ancestry vs. instances/class/superclass", "Nesting vs. [ancestry]", "Module nesting vs. subclassing", "Module namespacing vs. subclassing", "Being within a module vs. being a subclass of", "Parent vs. superclass", "Modspace vs. superclass", "Namespace vs. superclass"

  • parent, ancestors, namespace, modspace, modules, ...
  • subclass, superclass, inheritance, [class hierarchy?], instances, ...

http://svn.tylerrick.com/public/ruby/examples/ancestry_versus_instance_of.rb

puts '========================================'
puts 'Classes'

class C; end
class C2 < C; end

puts '-----------------'
puts 'Ancestry'
# C is *not* a subclass of Class!
puts C < Class
# (then why does it return nil instead of false?)

puts C2.superclass
puts C.superclass

puts C.ancestors.inspect
puts C2.ancestors.inspect

puts '-----------------'
puts 'Instance of'

# Different from checking whether they are *instances* of a class!
puts C.kind_of?(Class)
puts Class === C
puts C.class == Class

puts '-----------------'
puts 'Ancestry'

puts C2.superclass == C
# Shorthand:
puts C2 < C
puts C > C2

...

http://code.qualitysmith.com/gemables/qualitysmith_extensions/lib/qualitysmith_extensions/object/ancestry_of_method.rb


  def ancestry_of_method(method_name)
    method_name = method_name.to_s
    (self if self.methods(false).include?(method_name)) \
    ||
    if self.is_a?(Module)
      self.ancestors.find do |ancestor|
        ancestor.methods(false).include? method_name
      end or
      # The above search does not take into account *instance* methods provided by Class, Module, or Kernel.
      # Remember that ancestors and instances/class/superclass are different concepts, and that although classes/modules
      # do not have Class or Module as an "ancestor", they are still *instances* of Module or Class (which is a subclass of module).
      # self.ancestors does NOT include Class or Module, and yet we're still able to "inherit" instance methods from Class or Module.
      # So we have to do this extra search in case the method came from one of the instance methods of Class or Module or Kernel
      # (are there any other cases I'm missing?).
      begin
        # self.class.ancestors is usually [Class, Module, Object, PP::ObjectMixin, Kernel]
        self.class.ancestors.find do |ancestor|
          ancestor.instance_methods(false).include? method_name
          # || ancestor.private_instance_method_defined?( method_name.to_sym )
        end
      end
    else
      self.class.ancestry_of_instance_method(method_name)
    end
  end

[edit] In the context of MediaWiki

...


[edit]

Aliases: Namespacing vs. subclassing, Using namespaces or using inheritance, (many variations possible)

Personal tools