Sha256: bec337c81d864f11775ff0af285260b0dccfdb03817d0f615f47869a0a2677fa

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

class Object #:nodoc:
  def remove_subclasses_of(superclass)
    subclasses_of(superclass).each do |subclass|
      ObjectSpace.each_object(Class) do |k|
        if k.to_s == subclass
          k.instance_variables.each { |v| k.send(:remove_instance_variable, v) }
        end  
      end   
      Object.send(:remove_const, subclass) rescue nil
    end 
  end

  def subclasses_of(superclass)
    subclasses = []
    ObjectSpace.each_object(Class) do |k|
      next if !k.ancestors.include?(superclass) || superclass == k || k.to_s.include?("::") || subclasses.include?(k.to_s)
      subclasses << k.to_s
    end
    subclasses
  end

  # "", "   ", nil, and 0 are all blank
  def blank?
    if respond_to?(:empty?) && respond_to?(:strip)
      strip.empty?
    elsif respond_to? :empty?
      empty?
    elsif respond_to? :zero?
      zero?
    else
      !self
    end
  end
    
  def suppress(*exception_classes)
    begin yield
    rescue Exception => e
      raise unless exception_classes.any? {|cls| e.kind_of? cls}
    end
  end
end

class Class #:nodoc:
  def remove_subclasses
    Object.remove_subclasses_of(self)
  end

  def subclasses
    Object.subclasses_of(self)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activesupport-1.1.0 lib/active_support/core_ext/object_and_class.rb