Skip to content

Instantly share code, notes, and snippets.

@kevinclark
Created April 28, 2009 18:06
Show Gist options
  • Save kevinclark/103294 to your computer and use it in GitHub Desktop.
Save kevinclark/103294 to your computer and use it in GitHub Desktop.
-------------------------------------------------------- Class#inherited
inherited(subclass)
------------------------------------------------------------------------
Callback invoked whenever a subclass of the current class is
created.
Example:
class Foo
def self.inherited(subclass)
puts "New subclass: #{subclass}"
end
end
class Bar < Foo
end
class Baz < Bar
end
produces:
New subclass: Bar
New subclass: Baz
Clio:~ kev$ irb
>> class Foo; end
=> nil
>> class Bar < Foo; end
=> nil
>> class Baz < Foo; end
=> nil
>> ObjectSpace.each_object(Class) {|c| puts "Subclass of Foo: #{c.inspect}" if c < Foo }
Subclass of Foo: Baz
Subclass of Foo: Bar
=> 400
>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment