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

Ruby Eigenclass

mrzasa
June 28, 2016

Ruby Eigenclass

A presentation for a talk about Ruby eigenglass I gave during Ruby Talks at PGS Software. Ruby Talks are regular workshops of the Ruby team.

Main main source was Ruby Metaprogramming book: https://pragprog.com/book/ppmetr/metaprogramming-ruby

mrzasa

June 28, 2016
Tweet

More Decks by mrzasa

Other Decks in Programming

Transcript

  1. RUBY EIGENCLASS EVERYTHING YOU WANTED TO KNOW BUT YOU WERE

    AFRAID TO ASK Created by Maciej Rząsa
  2. SINGLETON METHODS Methods defined for a single instance of a

    class: c l a s s C a t d e f s o u n d " m e o w " e n d e n d C a t . n e w . s o u n d # > " m e o w " b o r e d _ c a t = C a t . n e w d e f b o r e d _ c a t . m o v e " s o l a z y . . . C a n ' t m o v e . . . " e n d b o r e d _ c a t . m o v e # > " S o l a z y . . . C a n ' t m o v e . . . "
  3. DIFFERENT SYNTAX b o r e d _ c a

    t = C a t . n e w c l a s s < < b o r e d _ c a t d e f r u n " A r e y o u k i d d i n g m e ? " e n d e n d Familiar, isn't it?
  4. CLASS METHODS Class methods are singleton methods of the class

    object c l a s s C a t c l a s s < < s e l f d e f n a m e " C a t " e n d e n d d e f s e l f . t a g " [ # { n a m e } ] " e n d d e f s o u n d " m e o w " e n d e n d
  5. WHAT IS THE S E L F WE USED? e

    i g e n _ b o r e d = c l a s s < < b o r e d _ c a t s e l f e n d # > # < C l a s s : # < C a t : 0 x 0 0 0 0 0 0 0 3 e c 9 6 3 8 > > e i g e n _ c a t = c l a s s < < C a t s e l f e n d # > # < C l a s s : C a t > USEFUL SHORTHANDS e i g e n _ b o r e d = = b o r e d _ c a t . s i n g l e t o n _ c l a s s e i g e n _ c a t = = C a t . s i n g l e t o n _ c l a s s
  6. EIGENCLASS aka singleton class, metaclass a class of a single

    object keeps method definitions for the object cannot be instantiated
  7. SUPERCLASS OF EIGENCLASS c l a s s A n

    i m a l d e f s o u n d " a n y n o i s e " e n d e n d c l a s s C a t < A n i m a l d e f s o u n d " m e o w " e n d e n d b o r e d _ c a t = C a t . n e w b o r e d _ c a t . s i n g l e t o n _ c l a s s . s u p e r c l a s s # > C a t C a t . s i n g l e t o n _ c l a s s . s u p e r c l a s s # > < C l a s s : A n i m a l >
  8. WHY SHOULD I CARE? understanding of Ruby object model and

    method dispatch metaprogramming: 'all about self' (Yehuda) private class methods
  9. PRIVATE CLASS METHOD c l a s s C a

    t d e f s e l f . t a g n a m e e n d p r i v a t e d e f s e l f . n a m e " C a t " e n d e n d C a t . n a m e # > " C a t " c l a s s C a t c l a s s < < s e l f d e f t a g n a m e e n d p r i v a t e d e f n a m e " C a t " e n d e n d e n d C a t . n a m e # ! N o M e t h o d E r r o r : p r i v a t e m e t h o d ` n a m e ' c a l l e d f o r C a t : C l a s s
  10. PRIVATE CLASS METHOD (2) c l a s s C

    a t d e f s e l f . t a g n a m e e n d d e f s e l f . n a m e " C a t " e n d p r i v a t e _ c l a s s _ m e t h o d : n a m e e n d C a t . n a m e # ! N o M e t h o d E r r o r : p r i v a t e m e t h o d ` n a m e ' c a l l e d f o r C a t : C l a s s