Skip to content

Instantly share code, notes, and snippets.

@dpaluy
Created December 24, 2012 16:12
Show Gist options
  • Save dpaluy/4369788 to your computer and use it in GitHub Desktop.
Save dpaluy/4369788 to your computer and use it in GitHub Desktop.
Don't subclass Struct classes
# 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]
@dpaluy
Copy link
Author

dpaluy commented Dec 24, 2012

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