r u b y / u s r / b i n / r u b y $ r u b y - v r u b y 1 . 9 . 3 p 4 4 8 ( 2 0 1 3 - 0 6 - 2 7 r e v i s i o n 4 1 6 7 5 ) [ x 8 6 _ 6 4 - d a r w i n 1 2 . 3 . 0 ] http://www.ruby-lang.org/en/downloads/ Pequenos exemplos: Interactive Ruby (irb) $ i r b ( i r b ) > p r i n t ' H e l l o W o r l d ! ' # H e l l o W o r l d ! 6 / 74
SublimeText, SciTE, Scribes, Emacs, Vim $ r u b y h e l l o . r b # H e l l o W o r l d ! ( i r b ) > l o a d ' h e l l o . r b ' # H e l l o W o r l d ! 7 / 74
Smalltalk + Lisp = Ruby 1993 - 1995 -> Ruby 0.95 2000 - primeiro livro em inglês 2004 - Rails Eu queria uma linguagem mais poderosa que o Perl e mais orientada a objeto que Python “ 9 / 74
i m e s { p r i n t ' Ê ' } i f D a t e . t o d a y . f r i d a y ? # Ê Ê Ê Ê Ê e x i t u n l e s s " r e s t a u r a n t e " . s t a r t _ w i t h ? " r e s t " Fácil de aprender Aprendi Ruby mais rápido do que qualquer outra linguagem, de um total de 30 ou 40; levei 3 dias para ficar mais confortável com Ruby do que com Perl, após 8 anos de Perl hacking. “ — Steve Yegge 11 / 74
= . n o m e = " F u l a n o d a S i l v a " _ p o s i ç ã o _ n o _ r a n k i n g = 7 O primeiro caracter não deve ser maiúsculo. v a l o r 1 = 1 2 3 3 v a l o r 2 = 5 7 9 3 t o t a l = v a l o r 1 + v a l o r 2 23 / 74
e = " J o h n " ( i r b ) > N o m e = " J o ã o " # w a r n i n g : a l r e a d y i n i t i a l i z e d c o n s t a n t N o m e ( i r b ) > N o m e = = " J o ã o " # t r u e P I = 3 . 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 M I N H A _ C O N S T A N T E = " t e s t " ( i r b ) > T R U E # t r u e 24 / 74
r i n t p r i n t ( ) p r i n t " H e l l o W o r l d ! " p r i n t ( " H e l l o W o r l d ! " ) p r i n t ( " H e l l o " , " " , " W o r l d " , " ! " ) p u t s " N o m e : " p u t s " O l á # { g e t s } ! " p u t s " N o m e : " n o m e = g e t s p u t s " O l á # { n o m e } ! " 25 / 74
* * 2 # 9 ( i r b ) > 1 0 - 4 / - 2 # 1 2 ( i r b ) > ( 1 0 - 4 ) / - 2 # - 3 ( i r b ) > 3 5 % 6 # 5 ( i r b ) > 1 . 3 4 + 0 . 1 2 3 # 1 . 4 6 3 ( i r b ) > 1 _ 7 4 7 _ 1 8 2 . 3 4 # 1 7 4 7 1 8 2 . 3 4 ( i r b ) > 3 > 5 # f a l s e ( i r b ) > 1 0 < = > 2 0 # - 1 ( i r b ) > 1 = = 1 # t r u e ( i r b ) > 1 ! = 1 # f a l s e 26 / 74
e r o ? # s e r i o u s ? ( i r b ) > 2 . e v e n ? # t r u e ( i r b ) > 0 . o d d ? # f a l s e ( i r b ) > 1 2 1 2 3 . t o _ s # " 1 2 1 2 3 " ( i r b ) > ( 1 + 2 ) . n e x t . n e x t # 5 ( i r b ) > 1 . c l a s s # F i x n u m ( i r b ) > 0 . 1 . c l a s s # F l o a t ( i r b ) > 1 _ 0 0 0 _ 0 0 0 _ 0 0 0 _ 0 0 0 _ 0 0 0 _ 0 0 0 _ 0 0 0 . c l a s s # B i g n u m 27 / 74
l d ! " ' O l á M u n d o ! ' ( i r b ) > " O r e s u l t a d o é # { 5 * * 2 } " " O r e s u l t a d o é 2 5 " " O i \ " M u n d o \ " ! " ( i r b ) > " O r e s u l t a d o é \ # { 5 * * 2 } " # " O r e s u l t a d o é \ # { 5 * * 2 } " ( i r b ) > " O l á " + " M u n d o " # " O l á M u n d o " ( i r b ) > ' 1 ' * 5 # 1 1 1 1 1 ( i r b ) > ' Ê ! ' * 5 # Ê ! Ê ! Ê ! Ê ! Ê ! 28 / 74
s a " . i n c l u d e ? " a s a " # t r u e ( i r b ) > " c a s a " . l e n g t h # 4 ( i r b ) > " c a s a " . g s u b ( " a " , " A " ) # " c A s A " ( i r b ) > " c a s a " . r s t r i p # " c a s a " ( i r b ) > " c a s a " . c o u n t ( " a " ) # 2 ( i r b ) > " 2 " . t o _ i # 2 ( i r b ) > " 2 " . t o _ f # 2 . 0 ( i r b ) > " 2 " . r j u s t ( 5 , " 0 " ) # 0 0 0 0 2 29 / 74
n h a c a s a " : " 1 " Único ( i r b ) > S y m b o l . a l l _ s y m b o l s . s i z e # 2 6 2 6 ( i r b ) > : c a s a ( i r b ) > S y m b o l . a l l _ s y m b o l s . s i z e # 2 6 2 7 Imutável ( i r b ) > : c a s a . c a p i t a l i z e ( i r b ) > S y m b o l . a l l _ s y m b o l s . s i z e # 2 6 2 8 30 / 74
' b e n c h m a r k ' B e n c h m a r k . m e a s u r e { 1 0 _ 0 0 0 _ 0 0 0 . t i m e s { : t e s t } } . t o t a l # 0 . 6 4 B e n c h m a r k . m e a s u r e { 1 0 _ 0 0 0 _ 0 0 0 . t i m e s { " t e s t " } } . t o t a l # 1 . 6 2 B e n c h m a r k . m e a s u r e { 1 0 _ 0 0 0 _ 0 0 0 . t i m e s { : t e s t = = : t e s t } } . t o t a l # 1 . 0 1 B e n c h m a r k . m e a s u r e { 1 0 _ 0 0 0 _ 0 0 0 . t i m e s { " t e s t " = = " t e s t " } } . t o t a l # 3 . 1 8 Possui poucos métodos ( i r b ) > S t r i n g . i n s t a n c e _ m e t h o d s . s i z e - S y m b o l . i n s t a n c e _ m e t h o d s . s i z e # 8 3 ( i r b ) > : c a s a . s i z e # 4 ( i r b ) > : c a s a . e m p t y ? # f a l s e ( i r b ) > : c a s a . t o _ s # c a s a 31 / 74
c a r r o = n i l ( i r b ) > c a r r o . a c e l e r a ! # 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 ` a c e l e r a ! ' f o r n i l : N i l C l a s s ( i r b ) > c a r r o . c l a s s NilClass true/false t r u e ! = f a l s e # t r u e f a l s e . t o _ s # " f a l s e " ‘ n u f s a i d … t r u e . c l a s s # T r u e C l a s s 32 / 74
, " o i " , 3 3 , : t e s t ] p r i n t c o i s a s [ 0 ] # 1 c o i s a s . a t ( 2 ) c o i s a s . f e t c h ( 1 0 , " N ã o e n c o n t r a d o ! " ) c o i s a s [ - 3 ] c o i s a s . v a l u e s _ a t ( 1 , 3 ) c o i s a s < < " q u a l q u e r c o i s a " c o i s a s . i n s e r t ( - 1 , 3 ) c o i s a s . p u s h 7 c o i s a s + [ " a b c " ] c o i s a s * 2 c o i s a s . j o i n ( " , " ) c o i s a s . p o p c o i s a s . s h i f t c o i s a s . d e l e t e _ a t 4 c o i s a s . d e l e t e 4 33 / 74
e . n e w ( 2 , 5 ) # 2 . . 5 ( 0 . . . 1 0 ) . i n c l u d e ? 1 0 # f a l s e ( 5 0 . . . 1 0 _ 0 1 2 _ 8 2 1 . 8 4 ) = = = 8 1 9 2 9 1 2 # t r u e ( 1 . . 1 _ 0 0 0 _ 0 0 0 _ 0 0 0 ) . t o _ a . r e v e r s e D a t e . t o d a y . . D a t e . t o d a y . n e x t _ y e a r " A " . . " Z " ( - 1 . . 1 0 ) . c l a s s # R a n g e 35 / 74
h o m e : " i n d e x . h t m l " , a b o u t : " p a g e 2 . h t m l " } p a g i n a s [ : h o m e ] # " i n d e x . h t m l " p a g i n a s [ " h o m e " ] # n i l # R u b y 1 . 8 . * p a g i n a s = { : h o m e = > " i n d e x . h t m l " , : a b o u t = > " p a g e 2 . h t m l " } c o i s a s = { : o i = > " H e l l o ! " , " u m " = > 1 , 1 . 2 = > F l o a t , a b c : % w ( a b c ) } c o i s a s [ 1 . 2 ] # F l o a t c o i s a s [ : a b c ] [ 1 ] # " b " m a i s _ s i m p l e s = { a : 1 , b : 2 } m a i s _ s i m p l e s . v a l u e s # [ 1 , 2 ] m a i s _ s i m p l e s . k e y s # [ : a , : b ] m a i s _ s i m p l e s . t o _ a # [ [ : a , 1 ] , [ : b , 2 ] ] m a i s _ s i m p l e s [ : c ] = 3 36 / 74
c o l o r : " g r e e n " , w i d t h : 2 0 } c o n f i g = { d a t a b a s e : " m y s q l " } d i s p l a y _ t i t l e ( " L i v r o 2 " , o p t i o n s ) d e f a u l t = { c o l o r : " b l u e " , h e i g h t : 3 0 } d e f a u l t . m e r g e ( o p t i o n s ) # { : c o l o r = > " g r e e n " , : h e i g h t = > 3 0 , : w i d t h = > 2 0 } h = H a s h . n e w ( " v a z i o . . . " ) h [ : a ] = 1 p r i n t h [ : a ] # 1 p r i n t h [ : b ] # v a z i o . . . 37 / 74
r i n t ' 2 ' p r i n t ' 3 ' linha após linha E se eu desejar só imprimir '2' se hoje for terça? E se eu desejar imprimir '3' até o mar virar sertão? 39 / 74
t a d o = " n ã o a c e r t o u " i f g e t s . t o _ i = = 3 r e s u l t a d o = " a c e r t o u " e n d p u t s r e s u l t a d o r e s u l t a d o = " n ã o a c e r t o u " r e s u l t a d o = " a c e r t o u " i f g e t s . t o _ i = = 3 p u t s r e s u l t a d o i f g e t s . t o _ i = = 3 p u t s " a c e r t o u " e l s e p u t s " n ã o a c e r t o u " e n d p u t s g e t s . t o _ i = = 3 ? " a c e r t o u " : " n ã o a c e r t o u " i f g e t s . t o _ i = = 3 t h e n p u t s " a c e r t o u " e l s e p u t s " n a o a c e r t o u " e n d 41 / 74
d a = g e t s . t o _ i i f e n t r a d a = = 3 p u t s " a c e r t o u " e l s i f [ 2 , 4 ] . i n c l u d e ? e n t r a d a p u t s " q u a s e a c e r t o u " e l s e p u t s " n ã o a c e r t o u " e n d i f n i l p r i n t " n i l " e l s i f f a l s e p r i n t " f a l s e " e l s i f " " p r i n t " v a z i o " e l s i f 0 p r i n t " z e r o " e l s e p r i n t " n a d a " e n d # w a r n i n g : s t r i n g l i t e r a l i n c o n d i t i o n # v a z i o 42 / 74
l o ! ' u n l e s s T i m e . n o w . y e a r > 2 0 1 3 p r i n t ' H e l l o ! ' i f n o t T i m e . n o w . y e a r > 2 0 1 3 u n l e s s T i m e . n o w . y e a r > 2 0 1 3 p r i n t " H e l l o ! " e l s e p r i n t " G o o d b y e ! " e n d 43 / 74
d ( 1 0 ) + 1 c a s e v a l o r w h e n 1 t h e n " U H H ! " w h e n 2 , 3 t h e n " u h h ! " w h e n 4 , 5 , 6 t h e n " n a m é d i a " w h e n 7 , 8 t h e n " p a s s o u " w h e n 9 , 1 0 t h e n " C D F " e n d v a l o r = r a n d ( 1 0 ) + 1 c a s e v a l o r w h e n 1 " U H H ! " w h e n 2 , 3 " u h h ! " w h e n 4 , 5 , 6 " n a m é d i a " w h e n 7 , 8 " p a s s o u " w h e n 9 , 1 0 " C D F " e n d 44 / 74
a c a " , " l a r a n j a " ] [ r a n d ( 2 ) ] p r i n t c a s e v a l o r w h e n " j a c a " " 1 r e a l " w h e n " l a r a n j a " " 1 0 r e a l " e n d a n o = T i m e . n o w . y e a r c a s e w h e n a n o > 2 0 1 3 p r i n t " H e l l o ! " e l s e p r i n t " G o o d b y e ! " e n d 45 / 74
s ' 3 ' b r e a k e n d m a r = " a g u a " l o o p { p u t s ' 3 ' m a r . c h o p ! # r e m o v e ú l t i m o c a r a c t e r e b r e a k i f m a r . e m p t y ? } 47 / 74
" w h i l e ( m a r . s i z e > 0 ) d o p u t s ' 3 ' m a r . c h o p ! e n d m a r = " a g u a " b e g i n p u t s ' 3 ' m a r . c h o p ! e n d w h i l e ( m a r . s i z e > 0 ) 48 / 74
" b e g i n p u t s ' 3 ' m a r . c h o p ! e n d u n t i l ( m a r . e m p t y ? ) m a r = " a g u a " u n t i l ( m a r . e m p t y ? ) d o p u t s ' 3 ' m a r . c h o p ! e n d 49 / 74
" f o r g o t a i n " a g u a " . c h a r s d o p u t s g o t a e n d f o r l e t r a i n " A " . . " Z " d o p u t s l e t r a e n d f o r c h a v e , v a l o r i n { a : 1 , b : 2 } d o p u t s " # { c h a v e } = > # { v a l o r } " e n d 50 / 74
" f o r g o t a i n m a r . c h a r s d o p u t s g o t a e n d m a r . e a c h _ c h a r d o | g o t a | p u t s g o t a e n d 1 0 . t i m e s { p r i n t ' Ê ' } 1 0 . t i m e s d o p r i n t ' Ê ' e n d 1 0 . t i m e s { | i | p u t s i } [ " D r . F u l a n o " , " D r . S i c r a n o " ] . a l l ? { | p r o | p r o . s t a r t _ w i t h ? ( " D r . " ) } true 52 / 74
= P r o c . n e w { | p r o | p r o . s t a r t _ w i t h ? ( " D r . " ) } v a l i d a c a o . c a l l # 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 t a r t _ w i t h ? ' f o r n i l : N i l C l a s s v a l i d a c a o . c a l l ( " M e s t r e J a c k s o n " ) # f a l s e P r o c . n e w d o | e m a i l , t e l e f o n e | e m a i l . i n c l u d e ? ( " @ " ) & & t e l e f o n e . i n c l u d e ? ( " 7 3 " ) e n d 53 / 74
_ t o t a l ( v a l o r 1 , v a l o r 2 ) p r i n t v a l o r 1 + v a l o r 2 e n d p r i n t _ t o t a l ( 1 0 , 2 1 ) p r i n t m e d i a ( 1 0 , 3 0 ) d e f m e d i a ( v a l o r 1 , v a l o r 2 ) r e t u r n ( v a l o r 1 + v a l o r 2 ) / 2 e n d d e f m e d i a ( v a l o r 1 , v a l o r 2 ) ( v a l o r 1 + v a l o r 2 ) / 2 # r e t o r n a a ú l t i m a l i n h a e x e c u t a d a e n d p r i n t m e d i a ( 1 0 , 3 0 ) 55 / 74
f i g u r e _ s e r v i d o r ( s e r v i d o r , * c o m p u t a d o r e s ) c o m p u t a d o r e s . e a c h d o | c o m p u t a d o r | c o m p u t a d o r . s e r v i d o r = s e r v i d o r e n d e n d c o n f i g u r e _ s e r v i d o r ( m i n h a _ m a q u i n a , c o m p u t a d o r 1 , c o m p u t a d o r 2 , c o m p u t a d o r 3 ) d e f m e d i a ( * v a l o r e s ) v a l o r e s . r e d u c e ( : + ) / v a l o r e s . s i z e e n d 57 / 74
o _ v a l i d o ? ( l i s t a , & v a l i d a d o r ) l i s t a . e a c h d o | e l e m e n t o | r e t u r n f a l s e u n l e s s v a l i d a d o r . c a l l ( e l e m e n t o ) e n d r e t u r n t r u e e n d r e s u l t a d o 1 = t u d o _ v a l i d o ? ( [ " D r . F u l a n o " , " D r . S i c r a n o " ] ) d o | p r o | p r o . s t a r t _ w i t h ? ( " D r . " ) e n d p u t s r e s u l t a d o 1 # t r u e r e s u l t a d o 2 = t u d o _ v a l i d o ? ( [ " D r . F u l a n o " , " M e s t r e S i c r a n o " ] ) d o | p r o | p r o . s t a r t _ w i t h ? ( " D r . " ) e n d p u t s r e s u l t a d o 2 # f a l s e 58 / 74
o _ v a l i d o ? ( l i s t a ) l i s t a . e a c h d o | e l e m e n t o | r e t u r n f a l s e u n l e s s y i e l d ( e l e m e n t o ) e n d r e t u r n t r u e e n d b l o c k _ g i v e n ? d e f t u d o _ v a l i d o ? ( l i s t a ) r e t u r n t r u e i f l i s t a . e m p t y ? | | ! b l o c k . g i v e n ? l i s t a . e a c h d o | e l e m e n t o | r e t u r n f a l s e u n l e s s y i e l d ( e l e m e n t o ) e n d r e t u r n t r u e e n d 59 / 74
v a C a s a d o e l a " d e v e r i a t e r u m a p o r t a " d o c a s a . d e v e r i a t e r ( 1 ) . p o r t a e n d e n d RSpec d e s c r i b e C a s a d o i t " s h o u l d h a v e a d o o r " d o s u b j e c t . s h o u l d h a v e ( 1 ) . d o o r e n d e n d 60 / 74
e c o = A v i a o . n e w ( " I D 7 - 2 " ) o u t r o _ t e c o t e c o = A v i a o . n e w ( " B Z - 1 " ) m e u _ t e c o t e c o . a c e l e r a ( 2 0 ) m e u _ t e c o t e c o . a c e l e r a ( 9 0 ) o u t r o _ t e c o t e c o . a c e l e r a ( 9 0 ) m e u _ t e c o t e c o . d e c o l a ! o u t r o _ t e c o t e c o . d e c o l a ! p r i n t A v i a o . q u a n t i d a d e # 2 p r i n t m e u _ t e c o t e c o . v o a n d o ? # t r u e s e a v e l o c i d a d e > 1 0 0 p r i n t o u t r o _ t e c o t e c o . v o a n d o ? # f a l s e c l a s s A v i a o d e f i n i t i a l i z e ( r e g i s t r o ) @ r e g i s t r o = r e g i s t r o e n d e n d 63 / 74
a o @ @ c o n t a d o r = 0 d e f i n i t i a l i z e ( r e g i s t r o ) @ r e g i s t r o = r e g i s t r o @ v e l o c i d a d e = 0 @ @ c o n t a d o r + = 1 e n d d e f s e l f . q u a n t i d a d e # m é t o d o d e c l a s s e @ @ c o n t a d o r e n d e n d p r i n t A v i a o . q u a n t i d a d e 65 / 74
a o d e f i n i t i a l i z e ( r e g i s t r o ) @ r e g i s t r o = r e g i s t r o @ v e l o c i d a d e = 0 @ @ c o n t a d o r + = 1 e n d d e f s e l f . q u a n t i d a d e # m é t o d o d e c l a s s e @ @ c o n t a d o r e n d d e f a c e l e r a ( v e l = 1 ) @ v e l o c i d a d e + = v e l e n d d e f d e c o l a ! @ n o _ a r = t r u e i f v e l > 1 0 0 e n d d e f v o a n d o ? @ n o _ a r e n d e n d 66 / 74
a o @ @ c o n t a d o r = 0 a t t r _ a c c e s s o r : r e g i s t r o , : v e l o c i d a d e , : n o _ a r d e f i n i t i a l i z e ( i d ) s e l f . r e g i s t r o = i d @ v e l o c i d a d e = 0 @ @ c o n t a d o r + = 1 e n d d e f s e l f . q u a n t i d a d e ; @ @ c o n t a d o r ; e n d d e f a c e l e r a ( v e l = 1 ) @ v e l o c i d a d e + = v e l e n d d e f d e c o l a ! @ n o _ a r = t r u e i f v e l > 1 0 0 e n d d e f v o a n d o ? ; s e l f . n o _ a r e n d e n d a v i a o = A v i a o . n e w ( " t e s t " ) p u t s a v i a o . r e g i s t r o # t e s t p u t s a v i a o . n o _ a r = t r u e p u t s a v i a o . v o a n d o ? # t r u e 67 / 74
contador de Aviao c l a s s J a t o < A v i a o d e f d e c o l a ! @ n o _ a r = t r u e i f v e l > 2 0 0 e n d e n d m e u _ j a t i n h o = J a t o . n e w ( " A Z - 5 " ) m e u _ j a t i n h o . a c e l e r a ( 1 5 0 ) m e u _ j a t i n h o . d e c o l a ! m e u _ j a t i n h o . v o a n d o ? 68 / 74
o a t t r _ a c c e s s o r : v e l o c i d a d e # ( . . . ) e n d c l a s s J a t o < A v i a o a t t r _ a c c e s s o r : v e l o c i d a d e # ( . . . ) e n d Nitro! m o d u l e T u r b o d e f n i t r o ! @ v e l o c i d a d e + = 2 0 0 e n d e n d c l a s s J a t o < A v i a o i n c l u d e T u r b o e n d c l a s s B a r c o i n c l u d e T u r b o e n d 69 / 74
i b / a s t e r o i d . r b p r o j e t o / l i b / s p a c e s h i p . r b r u b y - I l i b g a m e . r b r e q u i r e ' a s t e r o i d e ' r e q u i r e ' s p a c e s h i p ' r u b y - I . g a m e . r b r e q u i r e ' l i b / a s t e r o i d ' r e q u i r e ' l i b / s p a c e s h i p ' r u b y g a m e . r b r e q u i r e _ r e l a t i v e ' l i b / a s t e r o i d ' r e q u i r e _ r e l a t i v e ' l i b / s p a c e s h i p ' 70 / 74
Neighborly's Humble Little Ruby Book Artigos Tour de babel developerWorks interviews Matz The Difference Between Ruby Symbols and Strings Foto Matz at Waza 2013 73 / 74