Skip to content

Commit c9929f7

Browse files
authored
Add rule about not extending Data.define (rubocop#917)
1 parent 7e54062 commit c9929f7

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

README.adoc

+21
Original file line numberDiff line numberDiff line change
@@ -3604,6 +3604,27 @@ end
36043604
Person = Struct.new(:first_name, :last_name)
36053605
----
36063606

3607+
=== Don't Extend `Data.define` [[no-extend-data-define]]
3608+
3609+
Don't extend an instance initialized by `Data.define`.
3610+
Extending it introduces a superfluous class level.
3611+
3612+
[source,ruby]
3613+
----
3614+
# bad
3615+
class Person < Data.define(:first_name, :last_name)
3616+
end
3617+
3618+
Person.ancestors
3619+
# => [Person, #<Class:0x0000000105abed88>, Data, Object, (...)]
3620+
3621+
# good
3622+
Person = Data.define(:first_name, :last_name)
3623+
3624+
Person.ancestors
3625+
# => [Person, Data, Object, (...)]
3626+
----
3627+
36073628
=== Duck Typing [[duck-typing]]
36083629

36093630
Prefer https://en.wikipedia.org/wiki/Duck_typing[duck-typing] over inheritance.

0 commit comments

Comments
 (0)