How Should We Use Struct? class Specialized < Struct.new(:whatever) # ... define custom methods here... end Specialized = Struct.new(:whatever) do # ... define custom methods here... end
An Extra Class • The anonymous class doesn’t tell us much • Code reloading may cause “TypeError: superclass mismatch…” [Specialized, #, Struct, Enumerable, Object, PP::ObjectMixin, Kernel, BasicObject] class Specialized < Struct.new(:whatever) # ... define custom methods here... end
The “super” Problem class Specialized < Struct.new(:whatever) def whatever super || :default end include SomeMixin end Specialized = Struct.new(:whatever) do def whatever self[:whatever] || :default end prepend SomeMixin end
The Point • It’s not about the one right way to code • It’s about what we learn in the discussion • This trivial example alone includes: • Code malleability • The ancestor class chain • The value of prepend