Ruby modules. RRUG #1

Ba17945a06aac247b06548d5afe341e8?s=47 mrzasa
October 20, 2016

Ruby modules. RRUG #1

Uses of Ruby modules. Presentation given during the first meeting of Rzeszów Ruby User Group.

Main uses are: namespacing, mixins, singletons and limited monkeypatching.

Based on: http://practicingruby.com/articles/uses-for-modules-1 (esp. the PDF version)

Ba17945a06aac247b06548d5afe341e8?s=128

mrzasa

October 20, 2016
Tweet

Transcript

  1. RUBY MODULES RZESZÓW RUBY USER GROUP #1 20.10.2016 Maciej Rząsa

    PGS Software @mjrzasa
  2. Zastosowanie namespace mixin singleton ograniczony monkey patching

  3. NAMESPACE # G r a p h l i b

    r a r y c l a s s N o d e # . . . e n d # X M L l i b r a r y c l a s s N o d e # . . . e n d N o d e . n e w źródło: youtube.com
  4. NAMESPACE # G r a p h l i b

    r a r y m o d u l e G r a p h c l a s s N o d e # . . . e n d e n d # X M L l i b r a r y m o d u l e X M L c l a s s N o d e # . . . e n d e n d G r a p h : : N o d e . n e w X M L : : N o d e . n e w
  5. MIXIN

  6. INCLUDE m o d u l e G r e

    e t e r d e f s a y _ h e l l o p u t s " H e l l o ! " e n d e n d c l a s s P e r s o n i n c l u d e G r e e t e r e n d p e r s o n = P e r s o n . n e w p e r s o n . s a y _ h e l l o # > " H e l l o ! "
  7. EXTEND CLASS c l a s s P e r

    s o n e x t e n d G r e e t e r e n d P e r s o n . s a y _ h e l l o # > " H e l l o ! " P e r s o n . n e w . s a y _ h e l l o # N o M e t h o d E r r o r : u n d e f i n e d m e t h o d ` s a y _ h e l l o ' . . .
  8. EXTEND OBJECT p e r s o n 1 =

    P e r s o n . n e w p e r s o n 2 = P e r s o n . n e w p e r s o n 1 . e x t e n d ( G r e e t e r ) p e r s o n 1 . s a y _ h e l l o # > " H e l l o ! " p e r s o n 2 . s a y _ h e l l o # N o M e t h o d E r r o r : u n d e f i n e d m e t h o d ` s a y _ h e l l o ' . . .
  9. 1.6 1.6 1.6 1.6 1.6 1.6 1.6 1.6 1.6 1.6

    1.6 CONCERN m o d u l e S o f t D e l e t e d e f d e l e t e @ d e l e t e d _ a t = T i m e . n o w s a v e e n d d e f d e l e t e d ? ! @ d e l e t e d _ a t . n i l ? e n d m o d u l e C l a s s M e t h o d s d e f d e l e t e d w h e r e ( ' d e l e t e d I S N O T N U L L ' ) e n d e n d e n d c l a s s P e r s o n i n c l u d e S o f t D e l e t e e x t e n d S o f t D e l e t e : : C l a s s M e t h o d s e n d p = P e r s o n . a c t i v e . f i r s t p . d e l e t e d ? 1.6 1.6 1.6 1.6 1.6 1.6
  10. 1.6 1.6 1.6 1.6 1.6 1.6 1.6 1.6 1.6 1.6

    1.6 1.6 CONCERN: INCLUDED m o d u l e S o f t D e l e t e d e f s e l f . i n c l u d e d ( b a s e ) s e l f . e x t e n d ( C l a s s M e t h o d s ) e n d d e f d e l e t e @ d e l e t e d _ a t = T i m e . n o w s a v e e n d d e f d e l e t e d ? ! @ d e l e t e d _ a t . n i l ? e n d m o d u l e C l a s s M e t h o d s d e f d e l e t e d w h e r e ( ' d e l e t e d I S N O T N U L L ' ) e n d e n d e n d c l a s s P e r s o n i n c l u d e S o f t D e l e t e e n d p = P e r s o n . a c t i v e . f i r s t p . d e l e t e d ? 1.6 1.6 1.6 1.6 1.6
  11. CONCERN: ACTIVE SUPPORT m o d u l e S

    o f t D e l e t e e x t e n d A c t i v e S u p p o r t : : C o n c e r n d e f d e l e t e @ d e l e t e d _ a t = T i m e . n o w s a v e e n d d e f d e l e t e d ? ! @ d e l e t e d _ a t . n i l ? e n d m o d u l e C l a s s M e t h o d s d e f d e l e t e d w h e r e ( ' d e l e t e d I S N O T N U L L ' ) e n d e n d e n d c l a s s P e r s o n i n c l u d e S o f t D e l e t e e n d p = P e r s o n . a c t i v e . f i r s t p . d e l e t e d ?
  12. MIXINY: ZASTOSOWANIE E n u m e r a b

    l e C o m p a r a b l e soft delete cache key
  13. SINGLETON m o d u l e M a t

    h d e f p o w e r ( x , e ) x * * e e n d e n d m o d u l e M a t h O p e r a t i o n s e x t e n d M a t h e n d M a t h O p e r a t i o n s . p o w ( 2 , 3 ) m o d u l e M a t h e x t e n d M a t h d e f p o w e r ( x , e ) x * * e e n d e n d M a t h . p o w ( 2 , 3 ) m o d u l e M a t h e x t e n d s e l f d e f p o w e r ( x , e ) x * * e e n d e n d M a t h . p o w ( 2 , 3 )
  14. LOKALNY MONKEY PATCHING m o d u l e P

    D F F i x # f i x P D F l i b r a r y b u g e n d c l a s s P D F : : D o c u m e n t i n c l u d e P D F F i x e n d P D F : : D o c u m e n t . n e w ( ) . e x t e n d ( P D F F i x )
  15. PODSUMOWANIE moduły: organizacja kodu i dziedziczenie reużywalność kodu trduności w

    testowaniu (?) great power, great responsibility Ruby: a programmer's best friend źródło: Practicing Ruby (pdf download)