Similar posts

Comments

releu commented 5 months ago

757fb0d5ec7560b6f25f5bd98eadc020?size=52

Why?

releu commented 5 months ago

757fb0d5ec7560b6f25f5bd98eadc020?size=52

one more unnecessary noname class?

makaroni4 commented 5 months ago

E302c3320cd14b02cbe237b479d7f884?size=52

Agree, what is the profit?

ademidov commented 5 months ago

41d62300ce108a4463fc4bc82ec33072?size=52

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:

class Wheel < Struct.new(:rim, :tire)
end
Wheel2 = Struct.new(:rim, :tire) do
end

p Wheel.ancestors # => [Wheel, #<Class:0x00000000ce7c28>, Struct, Enumerable, Object, Kernel, BasicObject]
p Wheel2.ancestors # => [Wheel2, Struct, Enumerable, Object, Kernel, BasicObject]

robgleeson commented 5 months ago

22ca530c1db575f0a62b68c1900d8399?size=52

Another reason is because you cannot use Kernel#load to re-load a file that creates a subclass of Struct (type mismatch, since your subclass already has a parent).

postmodern commented 5 months ago

66f5d5b64b951b3eeb8b6c34fcb69237?size=52

Read the documentation for Struct.new

Struct.new("Wheel", :rim, :tire)

redjazz96 commented 5 months ago

95bc6501f8452eb4b459bf2dc4de5a96?size=52

@postmodern Struct.new allows for both versions, shown by the example:

Struct.new("Wheel", :rim, :tire) # => Struct::Wheel
Wheel = Struct.new(:rim, :tire)  # => Wheel

postmodern commented 5 months ago

66f5d5b64b951b3eeb8b6c34fcb69237?size=52

Also, what if you need to add additional methods to your Struct, but don't want to write all that boilerplate initialization code? Should we be using something like Values or Virtus instead?

ademidov
makaroni4
postmodern
redjazz96
releu
robgleeson
styx