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

Things you can't do in Ruby

Things you can't do in Ruby

Piotr Niełacny

March 03, 2013
Tweet

Other Decks in Technology

Transcript

  1. Multi-inheritance c l a s s M y C l

    a s s < F i r s t C l a s s , S e c C l a s s e n d # # # S y n t a x E r r o r : ( i r b ) : 1 : s y n t a x e r r o r , u n e x p e c t e d ' , ' , e x p e c t i n g # # # ' ; ' o r ' \ n ' # # # c l a s s M y C l a s s < F i r s t C l a s s , S e c C l a s s
  2. Class mixin c l a s s M y C

    l a s s i n c l u d e A r r a y e n d # # # T y p e E r r o r : w r o n g a r g u m e n t t y p e C l a s s ( e x p e c t e d M o d u l e )
  3. Object unfreeze a = A r r a y .

    n e w # = > [ ] a < < " o b j e c t " # = > [ " o b j e c t " ] a . f r e e z e a . f r o z e n ? # = > t r u e a < < " o b j e c t 2 " # = > R u n t i m e E r r o r : c a n t m o d i f y f r o z e n A r r a y a . u n f r e e z e # # # 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 ` u n f r e e z e ' f o r [ " o b j e c t " ] : A r r a y
  4. Change object class o = O b j e c

    t . n e w o . c l a s s # = > O b j e c t o . c l a s s = A r r a y # # # 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 ` c l a s s = ' f o r # `
  5. Default argument (~coffescript) Should be equal to c l a

    s s D e f a u l t d e f i n i t i a l i z e ( @ n a m e ) ; e n d e n d # S y n t a x E r r o r : f o r m a l a r g u m e n t c a n n o t b e a n i n s t a n c e v a r i a b l e c l a s s D e f a u l t d e f i n i t i a l i z e ( n a m e = n i l ) @ n a m e = n a m e e n d e n d
  6. Clone MRI repository g i t c l o n

    e g i t : / / g i t h u b . c o m / r u b y / r u b y . g i t
  7. Fine file i n c l u d e /

    r u b y / r u b y . h
  8. In ruby.h file you can find R U B Y

    _ Q f a l s e = 0 , R U B Y _ Q t r u e = 2 , R U B Y _ Q n i l = 4 , R U B Y _ Q u n d e f = 6 , R U B Y _ T _ N O N E = 0 x 0 0 , R U B Y _ T _ O B J E C T = 0 x 0 1 , R U B Y _ T _ C L A S S = 0 x 0 2 , R U B Y _ T _ M O D U L E = 0 x 0 3 , R U B Y _ T _ F L O A T = 0 x 0 4 , R U B Y _ T _ S T R I N G = 0 x 0 5 , R U B Y _ T _ R E G E X P = 0 x 0 6 , R U B Y _ T _ A R R A Y = 0 x 0 7 , R U B Y _ T _ H A S H = 0 x 0 8 , R U B Y _ T _ S T R U C T = 0 x 0 9 , R U B Y _ T _ B I G N U M = 0 x 0 a , R U B Y _ T _ F I L E = 0 x 0 b ,
  9. Move this to normal ruby module R U B Y

    _ Q f a l s e = 0 , R U B Y _ Q t r u e = 2 , R U B Y _ Q n i l = 4 , R U B Y _ Q u n d e f = 6 , R U B Y _ T _ N O N E = 0 x 0 0 , R U B Y _ T _ O B J E C T = 0 x 0 1 , R U B Y _ T _ C L A S S = 0 x 0 2 , R U B Y _ T _ M O D U L E = 0 x 0 3 , R U B Y _ T _ F L O A T = 0 x 0 4 , R U B Y _ T _ S T R I N G = 0 x 0 5 , R U B Y _ T _ R E G E X P = 0 x 0 6 , R U B Y _ T _ A R R A Y = 0 x 0 7 , R U B Y _ T _ H A S H = 0 x 0 8 , R U B Y _ T _ S T R U C T = 0 x 0 9 , R U B Y _ T _ B I G N U M = 0 x 0 a , R U B Y _ T _ F I L E = 0 x 0 b , m o d u l e R u b y I n t e r n a l Q f a l s e = 0 Q t r u e = 2 Q n i l = 4 Q u n d e f = 6 T _ N O N E = 0 x 0 0 T _ O B J E C T = 0 x 0 1 T _ C L A S S = 0 x 0 2 T _ M O D U L E = 0 x 0 3 T _ F L O A T = 0 x 0 4 T _ S T R I N G = 0 x 0 5 T _ R E G E X P = 0 x 0 6 T _ A R R A Y = 0 x 0 7 T _ H A S H = 0 x 0 8 T _ S T R U C T = 0 x 0 9 T _ B I G N U M = 0 x 0 a T _ F I L E = 0 x 0 b e n d
  10. Find in ruby.h aliases for VALUE and ID t y

    p e d e f u n s i g n e d l o n g V A L U E ; t y p e d e f u n s i g n e d l o n g I D ;
  11. And once again move to normal ruby module t y

    p e d e f u n s i g n e d l o n g V A L U E ; t y p e d e f u n s i g n e d l o n g I D ; m o d u l e R u b y I n t e r n a l # a l i a s ? ! e n d
  12. We need to use DL (Dynamic Linker?) A bridge to

    the dlopen() or dynamic library linker function. r e q u i r e ' d l ' r e q u i r e ' d l / i m p o r t ' m o d u l e L i b S u m e x t e n d D L : : I m p o r t e r d l l o a d ' . / l i b s u m . s o ' e x t e r n ' d o u b l e s u m ( d o u b l e * , i n t ) ' e x t e r n ' d o u b l e s p l i t ( d o u b l e ) ' e n d
  13. And once again move to normal ruby module t y

    p e d e f u n s i g n e d l o n g V A L U E ; t y p e d e f u n s i g n e d l o n g I D ; r e q u i r e ' d l ' r e q u i r e ' d l / i m p o r t ' m o d u l e R u b y I n t e r n a l e x t e n d D L : : I m p o r t e r t y p e a l i a s " V A L U E " , " u n s i g n e d l o n g " t y p e a l i a s " I D " , " u n s i g n e d l o n g " e n d
  14. Next, structures s t r u c t R B

    a s i c { V A L U E f l a g s ; V A L U E k l a s s ; } ;
  15. And import s t r u c t R B

    a s i c { V A L U E f l a g s ; V A L U E k l a s s ; } ; m o d u l e R u b y I n t e r n a l s B a s i c = [ " V A L U E f l a g s " , " V A L U E k l a s s " ] R B a s i c = s t r u c t ( B a s i c ) e n d
  16. And import s t r u c t R O

    b j e c t { s t r u c t R B a s i c b a s i c ; u n i o n { s t r u c t { l o n g n u m i v ; V A L U E * i v p t r ; s t r u c t s t _ t a b l e * i v _ i n d e x _ t b l ; } h e a p ; V A L U E a r y [ R O B J E C T _ E M B E D _ L E N _ M A X ] ; } a s ; } ; m o d u l e R u b y I n t e r n a l o b j e c t _ s t r u c t = [ ' l o n g n u m i v ' , ' V A L U E * i v p t r ' , ' s t _ t a b l e * i v _ i n d e x _ t b l ' , ' V A L U E a r a [ 3 ] ' ] R O b j e c t = s t r u c t ( B a s i c + o b j e c t _ s t r u c t ) e n d
  17. Internal pointer of ruby object d e f i n

    t e r n a l _ p t r ( * a r g s ) p o s = s e l f . o b j e c t _ i d * 2 D L : : C P t r . n e w ( p o s , * a r g s ) e n d
  18. To internals! c l a s s O b j

    e c t d e f i n t e r n a l _ p t r ( * a r g s ) p o s = s e l f . o b j e c t _ i d * 2 D L : : C P t r . n e w ( p o s , * a r g s ) e n d d e f i n t e r n a l k l a s s = i n t e r n a l _ c l a s s # = > R u b y I n t e r n a l : : R O b j e c t r = k l a s s . n e w ( i n t e r n a l _ p t r ) # = > R u b y I n t e r n a l : : R O b j e c t : 0 x 0 0 0 0 0 r e n d e n d
  19. Multi-inheritance d e f t o _ m o d

    u l e r e s u l t = s e l f . c l o n e o = R u b y I n t e r n a l : : R O b j e c t . n e w ( r e s u l t . i n t e r n a l _ p t r ) o . f l a g s & = ~ R u b y I n t e r n a l : : F L _ S I N G L E T O N o . f l a g s & = ~ R u b y I n t e r n a l : : T _ M A S K o . f l a g s | = R u b y I n t e r n a l : : T _ M O D U L E o . k l a s s = M o d u l e . i n t e r n a l _ p t r . t o _ i r e t u r n r e s u l t e n d
  20. Unfreeze object d e f u n f r e

    e z e s e l f . i n t e r n a l . f l a g s & = ~ R u b y I n t e r n a l : : F L _ F R E E Z E r e t u r n s e l f e n d
  21. Change object class d e f c l a s

    s = ( o t h e r ) s e l f . i n t e r n a l . k l a s s = o t h e r . i n t e r n a l _ p t r . t o _ i e n d
  22. Default argument c l a s s D e f

    a u l t d e f i n i t i a l i z e ( @ n a m e ) ; e n d e n d
  23. | t I V A R { y y _

    e r r o r ( " f o r m a l a r g u m e n t c a n n o t b e a n i n s t a n c e v a r i a b l e " ) ; $ $ = 0 ; }
  24. c l a s s D e f a u

    l t a t t r _ a c c e s s o r : n a m e d e f i n i t i a l i z e ( @ n a m e ) ; e n d e n d D e f a u l t . n e w . n a m e = > n i l D e f a u l t . n e w ( " R u b y " ) . n a m e # = > m e t h o d ' i n i t i a l i z e ' : g i v e n 1 , # e x p e c t e d 0 ( A r g u m e n t E r r o r )
  25. What about AST? " d e f i n i

    t i a l i z e ( @ n a m e ) ; e n d " . t o _ a s t
  26. R u b i n i u s : :

    A S T : : D e f i n e : 0 x 1 8 4 e c @ a r g u m e n t s = R u b i n i u s : : A S T : : F o r m a l A r g u m e n t s 1 9 : 0 x 1 8 4 f 0 @ l i n e = 1 @ d e f a u l t s = n i l # I M P O R T A N T L I N E ! @ s p l a t = n i l @ n a m e s = [ ] @ o p t i o n a l = [ ] @ r e q u i r e d = [ ] > @ b o d y = R u b i n i u s : : A S T : : B l o c k : 0 x 1 8 4 f 8 @ a r r a y = [ R u b i n i u s : : A S T : : N i l L i t e r a l : 0 x 1 8 5 0 0 @ l i n e = 1 > ] @ l o c a l s = n i l > @ n a m e = : i n i t i a l i z e >
  27. AST for default argument " d e f i n

    i t i a l i z e ( n a m e = n i l ) ; e n d " . t o _ a s t
  28. AST for default argument R u b i n i

    u s : : A S T : : D e f i n e : 0 x 1 8 4 e c @ a r g u m e n t s = R u b i n i u s : : A S T : : F o r m a l A r g u m e n t s 1 9 : 0 x 1 8 4 f 0 @ d e f a u l t s = R u b i n i u s : : A S T : : D e f a u l t A r g u m e n t s : 0 x 3 3 4 5 0 @ a r g u m e n t s = [ R u b i n i u s : : A S T : : L o c a l V a r i a b l e A s s i g n m e n t : 0 x 3 3 4 5 c @ n a m e = : n a m e @ v a l u e = R u b i n i u s : : A S T : : N i l L i t e r a l : 0 x 3 3 4 6 0 @ l i n e = 1 ] > @ n a m e s = [ : n a m e ] @ o p t i o n a l = [ : n a m e ] @ b o d y = R u b i n i u s : : A S T : : B l o c k : 0 x 1 8 4 f 8 @ a r r a y = [ R u b i n i u s : : A S T : : N i l L i t e r a l : 0 x 1 8 5 0 0 @ l i n e = 1 ] @ n a m e = : i n i t i a l i z e >
  29. Debug c l a s s F o r m

    a l A r g u m e n t s 1 9 < F o r m a l A r g u m e n t s d e f i n i t i a l i z e ( l i n e , r e q u i r e d , o p t i o n a l , s p l a t , p o s t , b l o c k ) r e q u i r e d . i n s p e c t = > [ f a l s e ] # f a l s e ? ! # c o d e . . . e n d e n d
  30. $$ = 0 | t I V A R {

    $ $ = 0 ; } | t I V A R { $ $ = $ 1 ; }
  31. Debug After this change, everything starts to work. c l

    a s s F o r m a l A r g u m e n t s 1 9 < F o r m a l A r g u m e n t s d e f i n i t i a l i z e ( l i n e , r e q u i r e d , o p t i o n a l , s p l a t , p o s t , b l o c k ) r e q u i r e d . i n s p e c t = > [ : @ n a m e ] # Y E S ! # c o d e . . . e n d e n d
  32. :@name => :name i f a r g . t

    o _ s = ~ / ^ @ / d e f a u l t s < < a r g . t o _ s [ 1 . . - 1 ] . t o _ s y m e n d
  33. Empty method d e f i n i t i

    a l i z e ( @ n a m e ) ; e n d c l a s s B l o c k i f b l o c k . a r r a y . e m p t y ? b l o c k . a r r a y < < N i l L i t e r a l . n e w ( l i n e ) e n d e n d
  34. Custom empty method d e f i n i t

    i a l i z e ( @ n a m e ) @ n a m e = n a m e e n d i f @ a r g u m e n t s . d e f a u l t _ v a r i a b l e s @ a r g u m e n t s . d e f a u l t s . n a m e s . e a c h d o | a r g _ n a m e | b l o c k . a r r a y . u n s h i f t ( I n s t a n c e V a r i a b l e A s s i g n m e n t . n e w ( l i n e , " @ # { a r g _ n a m e } " . t o _ s y m , L o c a l V a r i a b l e A c c e s s . n e w ( l i n e , a r g _ n a m e ) ) ) e n d e l s i f b l o c k . a r r a y . e m p t y ? b l o c k . a r r a y < < N i l L i t e r a l . n e w ( l i n e ) e n d
  35. How to include class $ g e m i n

    s t a l l i n c l u d e r e q u i r e ' i n c l u d e ' c l a s s M y C l a s s i n c l u d e A r r a y . t o _ m e n d