Created
December 24, 2012 16:12
-
-
Save dpaluy/4369788 to your computer and use it in GitHub Desktop.
Don't subclass Struct classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Don't subclass Struct classes | |
# instead of: | |
class Wheel < Struct.new(:rim, :tire) | |
#... | |
end | |
#use | |
Wheel2 = Struct.new(:rim, :tire) do | |
# ... | |
end | |
# When subclassing from Struct.new(...), you actually do that: | |
# Wheel = Class.new(Struct.new(...)), i.e. create two classes instead one and pollute ancestors chain with anonymous class: | |
p Wheel.ancestors # => [Wheel, #<Class:0x00000000ce7c28>, Struct, Enumerable, Object, Kernel, BasicObject] | |
p Wheel2.ancestors # => [Wheel2, Struct, Enumerable, Object, Kernel, BasicObject] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Summary of http://gistflow.com/posts/566-don-t-subclass-struct-classes-please