`X = Struct.new(:x) do` is bad
The style guide suggest to use
X = Struct.new(:x) do
def method
end
end
# instead of
class Y < Struct.new(:y)
def method
end
end
I think this is a bad recommendation. Yes, the later creates an unnecessary anonymous class, but it uses the usual syntax to create classes and thus is easy to understand. Even Rubocop doesn't recognize the assignment syntax everywhere (E.g. Style/Documentation doesn't care about this).
Also, this leads to hard to understand errors:
X = Struct.new(:x) do
XX = true
end
defined?(X::XX) # => nil
defined?(XX) # => "constant"
class Y < Struct.new(:y)
YY = true
end
defined?(Y::YY) # => "constant"
defined?(YY) # => nil
This is possible, but is it better?
Z = Struct.new(:z)
class Z
ZZ = true
end
defined?(Z::ZZ) # => "constant"
defined?(ZZ) # => nil
Great points! It's pretty similar to the problem with X = Class.new, which many tools won't understand as a class definition.
Even Rubocop doesn't recognize the assignment syntax everywhere (E.g. Style/Documentation doesn't care about this)
Fixing this in RuboCop shouldn't be hard, btw. That being said we can probably relax the guide and add more details to that particular rule.