![Page 1: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/1.jpg)
OOPS v2.0.0-rc.1Aditya Godbole
@aagdbl
![Page 2: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/2.jpg)
Definitions
• Type - Set of methods
• Subtype(T) - Superset methods of the type T
• Class - Implementation of type + properties of implementing object
![Page 3: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/3.jpg)
Maintainability
![Page 4: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/4.jpg)
Maintainability
Re-use
![Page 5: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/5.jpg)
Maintainability
Re-use
of?
![Page 6: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/6.jpg)
![Page 7: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/7.jpg)
Behaviour
(not data)
![Page 8: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/8.jpg)
Maintainability
![Page 9: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/9.jpg)
Maintainability
• Re-use of behaviour
![Page 10: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/10.jpg)
Maintainability
• Re-use of behaviour
• Typing and Sub-typing
![Page 11: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/11.jpg)
Axes of maintainability
![Page 12: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/12.jpg)
Axes of maintainability
Don’t repeatyourself(DRY)
![Page 13: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/13.jpg)
Axes of maintainability
Don’t repeatyourself(DRY)
![Page 14: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/14.jpg)
Axes of maintainability
Don’t repeatyourself(DRY)
Don’t shoot yourself(DSY)
![Page 15: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/15.jpg)
Maintainability
• Re-use of behaviour
• Sub-typing
![Page 16: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/16.jpg)
Maintainability
• Re-use of behaviour
• Sub-typing Granularity
![Page 17: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/17.jpg)
![Page 18: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/18.jpg)
Composability
• Behaviour - Inject behaviour from multiple sources
• Types - Create superset types by combining smaller types
![Page 19: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/19.jpg)
Classes
• Re-use
• Multiple inheritance
• Subtyping (Polymorphism)
• Multiple inheritance
![Page 20: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/20.jpg)
Interfaces
![Page 21: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/21.jpg)
Interfaces
• Type definitions
![Page 22: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/22.jpg)
Interfaces
• Type definitions
• Composable
![Page 23: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/23.jpg)
DRY
DSY
Classes / Inheritance
InterfacesJava?
![Page 24: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/24.jpg)
DRY
DSY
Classes / Inheritance
Interfaces
Mixins /Modules
Java?
![Page 25: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/25.jpg)
DRY
DSY
Classes / Inheritance
Interfaces
Mixins /Modules
Java?
Ruby?
![Page 26: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/26.jpg)
DRY
DSY
Classes / Inheritance
Interfaces
Mixins /Modules
Java?
![Page 27: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/27.jpg)
DRY
DSY
Classes / Inheritance
Interfaces
Mixins /Modules
Java?
Ruby?
![Page 28: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/28.jpg)
DRY
DSY
Classes / Inheritance
Interfaces
Mixins /Modules
Java?
Ruby?Interfaces?
![Page 29: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/29.jpg)
gem install rubycube
require “cube”
Composable Units of BEhaviour
![Page 30: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/30.jpg)
![Page 31: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/31.jpg)
![Page 32: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/32.jpg)
DRY
DSY
Classes / Inheritance
Interfaces
Mixins /Modules
Java?
RubyInterfaces
![Page 33: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/33.jpg)
DRY
DSY
Classes / Inheritance
Interfaces
Mixins /Modules
Java?
![Page 34: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/34.jpg)
DRY
DSY
Classes / Inheritance
Interfaces
Mixins /Modules
Java?
RubyInterfaces
![Page 35: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/35.jpg)
DRY
DSY
Classes / Inheritance
Interfaces
Mixins /Modules
Java?
RubyInterfaces
??
![Page 36: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/36.jpg)
Conflicts
![Page 37: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/37.jpg)
![Page 38: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/38.jpg)
![Page 39: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/39.jpg)
required interface
![Page 40: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/40.jpg)
![Page 41: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/41.jpg)
(sum, pos, fact)
![Page 42: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/42.jpg)
![Page 43: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/43.jpg)
AdvancedCalc =
SimpleCalcImpl
ProductCalcT
StatsCalcT - [:product]
+
+
=> AdvancedCalculator
![Page 44: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/44.jpg)
![Page 45: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/45.jpg)
![Page 46: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/46.jpg)
Using Traits• Structural classes - Enforce structural integrity.
• Identify interactions - Create smallest possible interface
• Traits should be smaller or equal to interfaces
• If required - extend structural class to create glue class for required interface
• Compose using traits
![Page 47: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/47.jpg)
Specs
• Rspec “shared_examples”
• Compose specs using “it_behaves_like”
![Page 48: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/48.jpg)
Also achievable …
• Java 8+
• Golang
![Page 49: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/49.jpg)
DRY
DSY
Java 8
InterfacesTraits
Ruby Go
![Page 50: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby](https://reader031.vdocuments.us/reader031/viewer/2022030215/588951131a28abde5a8b73c9/html5/thumbnails/50.jpg)
__/\__
https://github.com/adityagodbole/rubycube
gem install rubycube
(please go and read the traits paper)