Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Inheritance in Ruby

mrzasa
April 27, 2015

Inheritance in Ruby

Explaination of Ruby inheritance and method dispatch. For Ruby Talks at PGS Software

mrzasa

April 27, 2015
Tweet

More Decks by mrzasa

Other Decks in Programming

Transcript

  1. CLASSES c l a s s A d e f

    f o o p r i n t " A " e n d e n d c l a s s B < A d e f f o o s u p e r p r i n t " B " e n d e n d p u t s B . n e w . f o o # > > " A B "
  2. MODULES m o d u l e I n c

    l u d e d e f f o o ; s u p e r ; p r i n t " I " ; e n d e n d m o d u l e P r e p e n d d e f f o o ; s u p e r ; p r i n t " P " ; e n d e n d c l a s s A d e f f o o ; p r i n t " A " ; e n d e n d c l a s s B < A i n c l u d e I n c l u d e p r e p e n d P r e p e n d d e f f o o ; s u p e r ; p r i n t " B " ; e n d e n d p u t s B . n e w . f o o # > > " A I B P "
  3. INCLUDE USAGE multiple inheritance (interface and implementation) use glue methods

    as interface (e.g. # e a c h for E n u m e r a b l e ) wrap inherited methods (aspect-oriented programming)
  4. WRAP INHERITED METHODS m o d u l e S

    a v e W r a p p e r d e f s a v e p u t s " s a v e c a l l i n g " s u p e r e n d e n d c l a s s A < A c t i v e R e c o r d : : B a s e i n c l u d e S a v e W r a p p e r e n d A . n e w . s a v e Strange? Rails is built this way ;-)
  5. PREPEND USAGE override methods not owned by the team (e.g.

    gems) alternative to reopening classes
  6. BONUS: DEFINING METHOD FOR ONE OBJECT m o d u

    l e I n c l u d e d e f f o o ; s u p e r ; p r i n t " I " ; e n d e n d m o d u l e P r e p e n d d e f f o o ; s u p e r ; p r i n t " P " ; e n d e n d c l a s s A d e f f o o ; s u p e r ; p r i n t " A " ; e n d e n d c l a s s B < A i n c l u d e I n c l u d e p r e p e n d P r e p e n d d e f f o o ; s u p e r ; p r i n t " B " ; e n d e n d b = B . n e w d e f b . f o o ; s u p e r ; p r i n t " N N N " e n d b . f o o # > > A I B P N N N
  7. DEFINING METHOD FOR ONE OBJECT: USAGE c l a s

    s A d e f s e l f . f o o p r i n t " A " e n d e n d
  8. PRIVATE c l a s s F o o p

    r i v a t e d e f b a r ; ' b a r ' e n d e n d c l a s s B a z < F o o d e f c h e c k _ b a r p u t s b a r p u t s s e l f . b a r r e s c u e p u t s ' s e l f p a r e n t N o M e t h o d E r r o r ' p u t s s e l f . q u x r e s c u e p u t s ' s e l f s e l f N o M e t h o d E r r o r ' p u t s P a r e n t . n e w . b a r r e s c u e p u t s ' n e w N o M e t h o d E r r o r ' p u t s P a r e n t . n e w . s e n d ( : b a r ) r e s c u e p u t s ' n e w N o M e t h o d E r r o r ' e n d p r i v a t e d e f q u x p u t s " q u x " e n d e n d B a z . n e w . c h e c k _ b a r # b a r # s e l f p a r e n t N o M e t h o d E r r o r # s e l f s e l f N o M e t h o d E r r o r # n e w N o M e t h o d E r r o r # b a r
  9. PROTECTED c l a s s F o o p

    r o t e c t e d d e f b a r ; ' b a r ' e n d e n d c l a s s B a z < F o o d e f c h e c k _ b a r p u t s b a r p u t s s e l f . b a r r e s c u e p u t s ' s e l f p a r e n t N o M e t h o d E r r o r ' p u t s s e l f . q u x r e s c u e p u t s ' s e l f s e l f N o M e t h o d E r r o r ' p u t s P a r e n t . n e w . b a r r e s c u e p u t s ' n e w N o M e t h o d E r r o r ' e n d p r o t e c t e d d e f q u x " q u x " e n d e n d B a z . n e w . c h e c k _ b a r # b a r # b a r # q u x # n e w N o M e t h o d E r r o r
  10. USAGE Protected comparison mutator methods for immutable objects internal API

    for an object (e.g. A c t i o n C o n t r o l l e r : : C a c h i n g ) Private Not tested methods Internals Other use # p u b l i c _ s e n d if in doubt ;-)