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

Usando ruby em (quase) qualquer lugar

Usando ruby em (quase) qualquer lugar

Uma talk sobre mruby apresentada na Rupy Campinas 2015.

Oscar Esgalha

June 20, 2015
Tweet

More Decks by Oscar Esgalha

Other Decks in Technology

Transcript

  1. RUBIES Nome Tech Nome Tech MRI C JRuby JVM Rubinius

    C++ Rubymotion iOS MacRuby OSX IronRuby .NET MagLev Gemstone Topaz PyPy Opal Javascript Cardinal Parrot
  2. QUALQUER LUGAR Único requisito é C99 “ On the current

    version of mruby, it is not enough considered to support 16-bits integer architectures. ” https://github.com/mruby/mruby/wiki/FAQ-%28General-Topic%29
  3. “ Why should you switch to Ruby? If you are

    happy with Perl or Python, you don't have to. But if you do feel there must be a better language, Ruby may be your language of choice. Learning a new language is harmless. It gives you new ideas and insights. You don't have to switch, just learn and try it. You may find yourself comfortable enough with Ruby to decide to switch to it.” - Matz http://www.linuxdevcenter.com/pub/a/linux/2001/11/29/ruby.html
  4. DIFERENÇAS # r u b y 1 / 2 #

    = > 0 # m r u b y 1 / 2 # = > 0 . 5
  5. DIFERENÇAS # r u b y 1 / 0 #

    = > Z e r o D i v i s i o n E r r o r : d i v i d e d b y 0 1 % 0 # = > Z e r o D i v i s i o n E r r o r : d i v i d e d b y 0 0 / 0 # = > Z e r o D i v i s i o n E r r o r : d i v i d e d b y 0 # m r u b y 1 / 0 # = > i n f 1 % 0 # = > N a N 0 / 0 # = > N a N
  6. DIFERENÇAS Sem IO # r u b y ` e

    c h o " H u z z a h ! " ` # = > " H u z z a h ! \ n " # m r u b y ` e c h o " H u z z a h ! " ` # = > b a c k q u o t e s n o t i m p l e m e n t e d ( N o t I m p l e m e n t e d E r r o r ) https://github.com/iij/mruby-io
  7. DIFERENÇAS keyword arguments # r u b y 2 .

    0 d e f s a y ( w h a t : ' h e l l o ' ) p u t s w h a t e n d # m r u b y d e f s a y ( o p t i o n s = { } ) p u t s o p t i o n s . f e t c h ( : w h a t , ' h e l l o ' ) e n d
  8. DIFERENÇAS refinements # r u b y 2 . 1

    m o d u l e S c r e a m e r s r e f i n e S t r i n g d o d e f s c r e a m r e t u r n s e l f . u p c a s e e n d e n d e n d c l a s s P e r s o n u s i n g S c r e a m e r s e n d
  9. DIFERENÇAS Regex não incluso # r u b y "

    R $ 1 0 , 9 9 " [ / [ \ d | , ] + / ] # = > " 1 0 , 9 9 " # m r u b y " R $ 1 0 , 9 9 " [ / [ \ d | , ] + / ] # u n i n i t i a l i z e d c o n s t a n t R e g e x p ( N a m e E r r o r )
  10. BÁSICO g i t c l o n e -

    - d e p t h = 1 - - b r a n c h 1 . 1 . 0 h t t p s : / / g i t h u b . c o m / m r u b y / m r u b y . g i t c d m r u b y / m a k e l s b i n # = > m i r b , m r b c , m r d b , m r u b y , m r u b y - s t r i p
  11. BÁSICO mrbc c d . . c a t <

    < E O F > > s c r i p t . r b p u t s " t e s t \ n " E O F . / m r u b y / b i n / m r b c - o s c r i p t . m r b s c r i p t . r b
  12. BÁSICO mrbc . / m r u b y /

    b i n / m r b c - v - o s c r i p t . m r b s c r i p t . r b m r u b y 1 . 1 . 0 ( 2 0 1 4 - 1 1 - 1 9 ) 0 0 0 0 1 N O D E _ S C O P E : 0 0 0 0 1 N O D E _ B E G I N : 0 0 0 0 1 N O D E _ C A L L : 0 0 0 0 1 N O D E _ S E L F 0 0 0 0 1 m e t h o d = ' p u t s ' ( 2 1 8 ) 0 0 0 0 1 a r g s : 0 0 0 0 1 N O D E _ S T R " t e s t " l e n 5 i r e p 0 x 7 f 9 c d 0 4 0 d 7 d 0 n r e g s = 4 n l o c a l s = 1 p o o l s = 1 s y m s = 1 r e p s = 0 f i l e : s c r i p t . r b 1 0 0 0 O P _ L O A D S E L F R 1 1 0 0 1 O P _ S T R I N G R 2 L ( 0 ) ; " t e s t \ n " 1 0 0 2 O P _ S E N D R 1 : p u t s 1 1 0 0 3 O P _ S T O P
  13. BÁSICO mrdb c a t m r u b y

    / d o c / d e b u g g e r / R E A D M E . m d
  14. DIZENDO OLÁ PARA O MUNDO . / m r u

    b y / b i n / m i r b m i r b - E m b e d d a b l e I n t e r a c t i v e R u b y S h e l l > p u t s " h e l l o w o r l d \ n " h e l l o w o r l d
  15. DIZENDO OLÁ PARA O MUNDO c a t < <

    E O F > > h i . r b p u t s " h e l l o w o r l d \ n " E O F . / m r u b y / b i n / m r u b y h i . r b # = > h e l l o w o r l d
  16. DIZENDO OLÁ PARA O MUNDO . / m r u

    b y / b i n / m r b c h i . r b . / m r u b y / b i n / m r u b y - b h i . m r b # = > h e l l o w o r l d h e x d u m p - C h i . m r b 0 0 0 0 0 0 0 0 5 2 4 9 5 4 4 5 3 0 3 0 3 0 3 3 a 1 b 9 0 0 0 0 0 0 6 4 4 d 4 1 | R I T E 0 0 0 3 . . . . . d M A 0 0 0 0 0 0 1 0 5 4 5 a 3 0 3 0 3 0 3 0 4 9 5 2 4 5 5 0 0 0 0 0 0 0 4 6 3 0 3 0 | T Z 0 0 0 0 I R E P 0 0 0 0 0 0 2 0 3 0 3 0 0 0 0 0 0 0 3 e 0 0 0 1 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 4 | 0 0 . . . > . . . . . . . . . . 0 0 0 0 0 0 3 0 0 0 8 0 0 0 0 6 0 1 0 0 0 0 3 d 0 0 8 0 0 0 a 0 0 0 0 0 0 0 4 a | . . . . . . . = . . . . . . . 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 1 0 0 0 0 0 a 4 f 6 c 6 1 2 0 6 d 7 5 6 e 6 4 6 f | . . . . . . . O l a 0 0 0 0 0 0 5 0 0 a 0 0 0 0 0 0 0 1 0 0 0 4 7 0 7 5 7 4 7 3 0 0 4 5 4 e 4 4 0 0 | . . . . . . . p u t s . 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 8 | . . . . | 0 0 0 0 0 0 6 4
  17. DIZENDO OLÁ PARA O MUNDO . / m r u

    b y / b i n / m r b c - B h e l l o h i . r b
  18. DIZENDO OLÁ PARA O MUNDO # i n c l

    u d e < s t d i n t . h > c o n s t u i n t 8 _ t h e l l o [ ] = { 0 x 4 5 , 0 x 5 4 , 0 x 4 9 , 0 x 5 2 , 0 x 3 0 , 0 x 3 0 , 0 x 3 0 , 0 x 3 3 , 0 x c a , 0 x f 6 , 0 x 0 0 , 0 x 0 0 , 0 x 0 0 , 0 x 6 4 , 0 x 5 4 , 0 x 5 a , 0 x 3 0 , 0 x 3 0 , 0 x 3 0 , 0 x 3 0 , 0 x 4 9 , 0 x 5 2 , 0 x 4 5 , 0 x 5 0 , 0 x 0 0 , 0 x 0 0 , 0 x 0 0 , 0 x 4 6 , 0 x 3 0 , 0 x 3 0 , 0 x 0 0 , 0 x 0 0 , 0 x 0 0 , 0 x 3 e , 0 x 0 0 , 0 x 0 1 , 0 x 0 0 , 0 x 0 4 , 0 x 0 0 , 0 x 0 0 , 0 x 0 0 , 0 x 0 0 , 0 x 0 6 , 0 x 0 0 , 0 x 8 0 , 0 x 0 0 , 0 x 3 d , 0 x 0 0 , 0 x 0 0 , 0 x 0 1 , 0 x a 0 , 0 x 0 0 , 0 x 8 0 , 0 x 0 0 , 0 x 4 a , 0 x 0 0 , 0 x 0 0 , 0 x 0 0 , 0 x 0 0 , 0 x 0 1 , 0 x 0 0 , 0 x 0 0 , 0 x 0 a , 0 x 4 f , 0 x 6 c , 0 x 6 1 , 0 x 2 0 , 0 x 6 d , 0 x 7 5 , 0 x 6 e , 0 x 0 a , 0 x 0 0 , 0 x 0 0 , 0 x 0 0 , 0 x 0 1 , 0 x 0 0 , 0 x 0 4 , 0 x 7 0 , 0 x 7 5 , 0 x 7 4 , 0 x 7 3 , 0 x 0 0 , 0 x 4 5 , 0 x 4 e , 0 x 0 0 , 0 x 0 0 , 0 x 0 0 , 0 x 0 8 , } ;
  19. DIZENDO OLÁ PARA O MUNDO # i n c l

    u d e < s t d i n t . h > c o n s t u i n t 8 _ t h e l l o [ ] = { / * c o i s a s * / } ;
  20. DIZENDO OLÁ PARA O MUNDO # i n c l

    u d e < s t d i n t . h > # i n c l u d e < m r u b y . h > # i n c l u d e < m r u b y / i r e p . h > c o n s t u i n t 8 _ t h e l l o [ ] = { / * c o i s a s * / } ; i n t m a i n ( i n t a r g c , c h a r * * a r g v ) { m r b _ s t a t e * m r b = m r b _ o p e n ( ) ; m r b _ l o a d _ i r e p ( m r b , h e l l o ) ; m r b _ c l o s e ( m r b ) ; r e t u r n 0 ; }
  21. DIZENDO OLÁ PARA O MUNDO g c c h i

    . c m r u b y / b u i l d / h o s t / l i b / l i b m r u b y . a - o h i . / h i # = > h e l l o w o r l d
  22. A MODULARIDADE DO MRUBY M R u b y :

    : B u i l d . n e w d o | c o n f | e n d
  23. A MODULARIDADE DO MRUBY M R u b y :

    : B u i l d . n e w d o | c o n f | t o o l c h a i n : g c c # t o o l c h a i n : c l a n g # t o o l c h a i n : v i s u a l c p p # t o o l c h a i n : a n d r o i d e a b i e n d
  24. A MODULARIDADE DO MRUBY M R u b y :

    : B u i l d . n e w d o | c o n f | c o n f . f i l e _ s e p a r a t o r = ' / ' # c o m p i l e r c o n f . c c d o | c c | c c . c o m m a n d = . . . c c . f l a g s = . . . e n d c o n f . l i n k e r { | l i n k e r | . . . } c o n f . a r c h i v e r { | a r c h i v e r | . . . } c o n f . y a c c { | y a c c | . . . } c o n f . e x t s { | e x t s | . . . } e n d
  25. A MODULARIDADE DO MRUBY M R u b y :

    : B u i l d . n e w d o | c o n f | c o n f . g e m ' c a m i n h o / a b s o l u t o / p a r a / m i n h a / m g e m ' c o n f . g e m g i t h u b : ' i i j / m r u b y - i o ' c o n f . g e m g i t : ' g i t @ g i t h u b . c o m : m a t s u m o t o - r / m r u b y - s l e e p . g i t ' c o n f . g e m b i t b u c k e t : ' t c h u b i r u b i / a l g u m a - g e m ' e n d
  26. A MODULARIDADE DO MRUBY M R u b y :

    : B u i l d . n e w d o | c o n f | c o n f . g e m b o x ' d e f a u l t ' # c o n f . g e m b o x ' f u l l - c o r e ' e n d
  27. A MODULARIDADE DO MRUBY M R u b y :

    : B u i l d . n e w ( ' o u t r a - p l a t a f o r m a ' ) d o | c o n f | c o n f . g e m b o x ' g e m a s - d a - o u t r a - p l a t a f o r m a ' e n d
  28. A MODULARIDADE DO MRUBY M R u b y :

    : G e m B o x . n e w d o | c o n f | c o n f . g e m . . . c o n f . g e m . . . . . . e n d
  29. MRBGEMS ─ ─ m r u b y - c

    o o l - e x t - n o m e d a g e m │ ├ ─ i n c l u d e / - h e a d e r s │ ├ ─ m r b l i b / - c ó d i g o d a l i b e m r u b y │ ├ ─ s r c / - c ó d i g o d a l i b e m C │ ├ ─ t e s t / - t e s t e s ( r u b y ) │ ├ ─ m r b g e m . r a k e - s p e c s d a g e m │ ├ ─ R E A D M E . m d - d o c u m e n t a ç ã o
  30. MRBGEMS mrb-cool-ext/mrbgem.rake M R u b y : : G

    e m : : S p e c i f i c a t i o n . n e w ( ' m r u b y - c o o l - e x t ' ) d o | s p e c | s p e c . l i c e n s e = ' M I T ' s p e c . a u t h o r = ' R u p y e r ' s p e c . l i n k e r . f l a g s _ b e f o r e _ l i b r a r i e s < < " - l n c u r s e s " s p e c . a d d _ d e p e n d e n c y ( ' m r u b y - o n i g - r e g e x p ' , g i t h u b : ' m a t t n / m r u b y - o n i g - r e g e x p ' e n d
  31. MRBGEMS mruby-cool-ext/mrblib/extension.rb c l a s s P e r

    s o n a t t r _ a c c e s s o r : a g e , : n a m e d e f i n i t i a l i z e @ a g e = 0 @ n a m e = " n o n e " e n d e n d
  32. MRBGEMS mruby-cool-ext/src/extension.c # i n c l u d e

    " m r u b y . h " v o i d m r b _ m r u b y _ c o o l _ e x t _ g e m _ i n i t ( m r b _ s t a t e * m r b ) { / / f a z e r a s c o i s a s } v o i d m r b _ m r u b y _ c o o l _ e x t _ g e m _ f i n a l ( m r b _ s t a t e * m r b ) { / / l i b e r a r m e m o r i a }
  33. MRBGEMS mruby-cool-ext/src/extension.c v o i d m r b _

    m r u b y _ c o o l _ e x t _ g e m _ i n i t ( m r b _ s t a t e * m r b ) { / * m o d u l e A n i m a l * / s t r u c t R C l a s s * a n i m a l = m r b _ d e f i n e _ m o d u l e ( m r b , " A n i m a l " ) ; / * c l a s s B o m b a * / s t r u c t R C l a s s * b o m b = m r b _ d e f i n e _ c l a s s ( m r b , " B o m b a " , m r b - > o b j e c t _ c l a s s ) ; }
  34. MRBGEMS mruby-cool-ext/src/extension.c v o i d m r b _

    m r u b y _ c o o l _ e x t _ g e m _ i n i t ( m r b _ s t a t e * m r b ) { / * c l a s s L i s t a < A r r a y * / s t r u c t R C l a s s * a r r a y = m r b _ c l a s s _ g e t ( m r b , " A r r a y " ) ; s t r u c t R C l a s s * l i s t a = m r b _ d e f i n e _ c l a s s ( m r b , " L i s t a " , a r r a y ) ; }
  35. MRBGEMS mruby-cool-ext/src/extension.c v o i d m r b _

    m r u b y _ c o o l _ e x t _ g e m _ i n i t ( m r b _ s t a t e * m r b ) { / * m o d u l e A n i m a l * / s t r u c t R C l a s s * a n i m a l = m r b _ d e f i n e _ m o d u l e ( m r b , " A n i m a l " ) ; / * A n i m a l : : P A W S _ C O U N T - > 4 * / m r b _ d e f i n e _ c o n s t ( m r b , a n i m a l , " P A W S _ C O U N T " , m r b _ f i x n u m _ v a l u e ( 4 ) ) ; / * A n i m a l : : S O U N D - > " r o a r " * / m r b _ d e f i n e _ c o n s t ( m r b , a n i m a l , " S O U N D " , m r b _ s t r _ n e w _ c s t r ( m r b , " r o a r " ) ) ; }
  36. MRBGEMS mruby-cool-ext/src/extension.c s t a t i c m r

    b _ v a l u e m r b _ b o m b _ e x p l o d e ( m r b _ s t a t e * m r b , m r b _ v a l u e s e l f ) { r e t u r n m r b _ s t r _ n e w _ c s t r ( m r b , " B O O M ! " ) ; }
  37. MRBGEMS mruby-cool-ext/src/extension.c v o i d m r b _

    m r u b y _ c o o l _ e x t _ g e m _ i n i t ( m r b _ s t a t e * m r b ) { / * c l a s s B o m b a * / s t r u c t R C l a s s * b o m b = m r b _ d e f i n e _ c l a s s ( m r b , " B o m b a " , m r b - > o b j e c t _ c l a s s ) ; / * B o m b a # e x p l o d e * / m r b _ d e f i n e _ m e t h o d ( m r b , b o m b , " e x p l o d i r " , m r b _ b o m b _ e x p l o d e , M R B _ A R G S _ N O N E ( ) ) ; }
  38. MRBGEMS mruby-cool-ext/src/extension.c # i n c l u d e

    " m r u b y / v a r i a b l e . h " s t a t i c m r b _ v a l u e m r b _ b o m b _ p l a n t ( m r b _ s t a t e * m r b , m r b _ v a l u e s e l f ) { m r b _ i n t t i m e _ v a l u e ; m r b _ g e t _ a r g s ( m r b , " i " , & t i m e _ v a l u e ) ; m r b _ s y m t i m e _ s y m = m r b _ i n t e r n _ c s t r ( m r b , " @ t e m p o " ) ; m r b _ i v _ s e t ( m r b , s e l f , t i m e _ s y m , m r b _ f i x n u m _ v a l u e ( t i m e _ v a l u e ) ) ; r e t u r n m r b _ n i l _ v a l u e ( ) ; }
  39. MRBGEMS mruby-cool-ext/src/extension.c v o i d m r b _

    m r u b y _ c o o l _ e x t _ g e m _ i n i t ( m r b _ s t a t e * m r b ) { / * c l a s s B o m b a * / s t r u c t R C l a s s * b o m b = m r b _ d e f i n e _ c l a s s ( m r b , " B o m b a " , m r b - > o b j e c t _ c l a s s ) ; / * B o m b a # a r m a r * / m r b _ d e f i n e _ m e t h o d ( m r b , b o m b , " a r m a r " , m r b _ b o m b _ p l a n t , M R B _ A R G S _ R E Q ( 1 ) ) ; }
  40. SOFTWARE ─ ─ s o f t w a r

    e │ ├ ─ c o d i g o . c - c ó d i g o f o n t e │ ├ ─ b u i l d _ c o n f i g . r b - c o n f i g u r a ç ã o m r u b y │ ├ ─ R E A D M E . m d - d o c u m e n t a ç ã o
  41. SOFTWARE # i n c l u d e <

    m r u b y . h > i n t m a i n ( i n t a r g c , c h a r * * a r g v ) { r e t u r n 0 ; }
  42. SOFTWARE i n t m a i n ( i

    n t a r g c , c h a r * * a r g v ) { r e t u r n 0 ; }
  43. SOFTWARE i n t m a i n ( i

    n t a r g c , c h a r * * a r g v ) { m r b _ s t a t e * m r b = m r b _ o p e n ( ) ; i f ( ! m r b ) { / * d e u r u i m * / } m r b _ c l o s e ( m r b ) ; r e t u r n 0 ; }
  44. SOFTWARE i n t m a i n ( i

    n t a r g c , c h a r * * a r g v ) { m r b _ s t a t e * m r b = m r b _ o p e n ( ) ; m r b _ c l o s e ( m r b ) ; r e t u r n 0 ; }
  45. SOFTWARE # i n c l u d e <

    m r u b y / c o m p i l e . h > i n t m a i n ( i n t a r g c , c h a r * * a r g v ) { m r b _ s t a t e * m r b = m r b _ o p e n ( ) ; m r b _ l o a d _ s t r i n g ( m r b , " p ' O i r u p y ! ' " ) ; m r b _ c l o s e ( m r b ) ; r e t u r n 0 ; }
  46. SOFTWARE i n t m a i n ( i

    n t a r g c , c h a r * * a r g v ) { m r b _ s t a t e * m r b = m r b _ o p e n ( ) ; m r b _ l o a d _ s t r i n g ( m r b , " p ' O i r u p y ! ' " ) ; m r b _ c l o s e ( m r b ) ; r e t u r n 0 ; }
  47. SOFTWARE g i t c l o n e -

    - d e p t h = 1 - - b r a n c h 1 . 1 . 0 h t t p s : / / g i t h u b . c o m / m r u b y / m r u b y . g i t c p b u i l d _ c o n f i g . r b m r u b y / b u i l d _ c o n f i g . r b m a k e
  48. SOFTWARE g c c c o d i g o

    . c m r u b y / b u i l d / h o s t / l i b / l i b m r u b y . a - o b i n / b i n a r i o
  49. SOFTWARE i n t m a i n ( i

    n t a r g c , c h a r * * a r g v ) { m r b _ s t a t e * m r b = m r b _ o p e n ( ) ; m r b _ l o a d _ s t r i n g ( m r b , " p ' O i r u p y ! ' " ) ; m r b _ c l o s e ( m r b ) ; r e t u r n 0 ; }
  50. SOFTWARE i n t m a i n ( i

    n t a r g c , c h a r * * a r g v ) { m r b _ s t a t e * m r b = m r b _ o p e n ( ) ; m r b _ l o a d _ s t r i n g ( m r b , " P r o g r a m a . e x e c u t a r ! " ) ; m r b _ c l o s e ( m r b ) ; r e t u r n 0 ; }
  51. SOFTWARE i n t m a i n ( i

    n t a r g c , c h a r * * a r g v ) { m r b _ s t a t e * m r b = m r b _ o p e n ( ) ; m r b _ v a l u e v = m r b _ l o a d _ s t r i n g ( m r b , " P r o g r a m a . e x e c u t a r ! " ) ; i f ( m r b - > e x c ) { i f ( ! m r b _ u n d e f _ p ( v ) ) { m r b _ p r i n t _ e r r o r ( m r b ) ; } } m r b _ c l o s e ( m r b ) ; r e t u r n 0 ; }
  52. SOFTWARE i n t m a i n ( i

    n t a r g c , c h a r * * a r g v ) { m r b _ s t a t e * m r b = m r b _ o p e n ( ) ; m r b _ l o a d _ s t r i n g ( m r b , " P r o g r a m a . e x e c u t a r ! " ) ; m r b _ c l o s e ( m r b ) ; r e t u r n 0 ; }
  53. SOFTWARE i n t m a i n ( i

    n t a r g c , c h a r * * a r g v ) { F I L E * r u b y F i l e ; m r b _ s t a t e * m r b = m r b _ o p e n ( ) ; m r b _ l o a d _ f i l e ( m r b , r u b y F i l e ) ; m r b _ c l o s e ( m r b ) ; r e t u r n 0 ; }
  54. SOFTWARE # i n c l u d e <

    m r u b y / c o m p i l e . h > - > R e m o v e r # i n c l u d e < m r u b y / d u m p . h > - > A d i c i o n a r i n t m a i n ( i n t a r g c , c h a r * * a r g v ) { F I L E * c o m p i l e d R u b y F i l e ; m r b _ s t a t e * m r b = m r b _ o p e n ( ) ; m r b _ l o a d _ i r e p _ f i l e ( m r b , c o m p i l e d R u b y F i l e ) ; m r b _ c l o s e ( m r b ) ; r e t u r n 0 ; }