Ruby

Ruby

Made by DeepSource

Unexpected override of built-in Struct method RB-W1007

Bug risk
Major

When creating a custom value object using Struct.new, be careful as to not have attributes whose names match the built-in methods provided by the Struct class.

Bad practice

Bad = Struct.new(:members, :clone, :count)
b = Bad.new([], true, 1)
b.members #=> [] (overriding `Struct#members`)
b.clone #=> true (overriding `Object#clone`)
b.count #=> 1 (overriding `Enumerable#count`)

Recommended

# Not using the built-in `members`, `clone` etc.
Good = Struct.new(:id, :name)
g = Good.new(1, "foo")
g.members #=> [:id, :name]
g.clone #=> #<struct Good id=1, name="foo">
g.count #=> 2

References

  1. How to Use Struct & OpenStruct in Ruby