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

Learning Ruby

Learning Ruby

David Francisco

September 26, 2011
Tweet

More Decks by David Francisco

Other Decks in Programming

Transcript

  1. def  why  do  i  need  to  learn  it?    

     ruby  on  rails  =  'Ruby  language'  +  'Rails  framework'      read  the  slides!  unless  u  likez  jsp   end
  2. def  what  makes  ruby  =  :special      features  =

     ['Object-­‐oriented']      features  <<  'Interpreted'      features  <<  'Open-­‐source'            #  And      features  <<  'Mixins'  #  somewhat  similar  to  multiple  inheritance      features  <<  "Native  threads"      features  <<  "Large  standard  library"      features  <<  "Centralized  package  management  through  RubyGems"      features  <<  "Built-­‐in  support  for  rational  and  complex  numbers"      features  <<  "Automatic  garbage  collection"      features  <<  "Default  arguments"      features  <<  "Literal  notation  for  arrays,  hashes,  regex  and  symbols"      features  <<  "Dynamic  typing  and  Duck  typing"      features  <<  "Operator  overloading"      features  <<  "Flexible  syntax  that  serves  as  a  foundation  for  DSL"   end  
  3. Basic concepts of Ruby if  (a  !=  null)  {...}  

    null.toString()  //  NPE     unless  a.nil?  {...}   nil.to_s  #  ""   -­‐4.abs    #  →  4   6.zero?  #  →  false  
  4. Types in Ruby puts  -­‐4   puts  0x5C1    

           #  1473  (Hex)   puts  01411            #  777  (Octal)   puts  12_000_000  #  12000000   puts  1.5   puts  12.043e-­‐04  #  0.0012043   puts  123456789101112131415  
  5. Types in Ruby puts  -­‐4.type          

                                 #  Fixnum   puts  0x5C1.type                                  #  Fixnum   puts  01411.type                                  #  Fixnum   puts  12_000_000.type                        #  Fixnum   puts  1.5.type                                      #  Float   puts  12.043e-­‐04.type                        #  Float   puts  123456789101112131415.type  #  Bignum  
  6. Types in Ruby -  Lightweight strings -  Escape sequences (\t,

    \n, …) -  Expression interpolation puts  'hello\tmiguel'  #  →  hello\tmiguel   puts  "hello\tmiguel"  #  →  hello    miguel   puts  "hello  #{'manolo  '  *3}"   #  hello  manolo  manolo  manolo   myvar.to_s  
  7. Types in Ruby mystring  =  <<LOREM      Donec  id

     elit  non  mi  porta  gravida  at  eget  metus.      Morbi  leo  risus,  porta  ac  consectetur  ac,  vestibulum      at  eros.  Curabitur  blandit  tempus  porttitor.      Vestibulum  id  ligula  porta  felis  euismod  semper.      Etiam  porta  sem  malesuada  magna  mollis  euismod.      Maecenas  sed  diam  eget  risus  varius  blandit  sit!     LOREM   %q{Text}  #  Single  quoted   %Q{Text}  #  Double  quoted  
  8. Types in Ruby do_this  if  query  ==  :get   link_to

     "View  Article",  :controller  =>  "articles",  :action  =>  "show"   :hello  
  9. Types in Ruby cookies  =  1..10   bad_cookies  =  1..5

      burnt_cookies  =  1..5     puts  bad_cookies  ==  cookies                  #  false   puts  bad_cookies  ==  burnt_cookies      #  true   puts  bad_cookies.eql?  burnt_cookies  #  true     myguess  =  2   puts  cookies  ===  myguess            #  true   puts  cookies.include?  myguess  #  true     puts  cookies.include?  2.23  #  true   puts  cookies.include?  2..4  #  false  
  10. Types in Ruby its_empty  =  []   oh_so_empty  =  Array.new

      hello  =  ['ni  hao',  'bonjour',  'hi',  'howdy',  'bom  dia']   random_types  =  [13,  'napkin',  (4+8+42).to_s]  
  11. Types in Ruby my_waiku  =  %W(he  is  nice  to  my

     #{2*7}  cats)   my_waiku.to_a  #  ["he",  "is",  "nice",  "to",  "my",  "14",  "cats"]     my_range  =  1..5   my_array  =  my_range.to_a  #  [1,  2,  3,  4,  5]     my_array[6]  =  7                  #  [1,  2,  3,  4,  5,  nil,  7]   my_array.insert(7,  8)      #  [1,  2,  3,  4,  5,  nil,  7,  8]   my_array.insert(-­‐1,  9)    #  [1,  2,  3,  4,  5,  nil,  7,  8,  9]     my_array.pop                        #  [1,  2,  3,  4,  5,  nil,  7,  8]   my_array.pop  3                    #  [1,  2,  3,  4,  5]   my_array.push  6,  7            #  [1,  2,  3,  4,  5,  6,  7]   my_array  <<  8  <<  9            #  [1,  2,  3,  4,  5,  6,  7,  8,  9]  
  12. Types in Ruby roles  =  Hash.new  'Not  here!'   roles

     =  {  'LA'  =>  'Gaspar',  'PM'  =>  'Tavares',  'CM'  =>  'Gaspar'  }     roles['QM']                              #  →  "Not  here!"   roles.has_key?  'LA'              #  →  true   roles.has_value?  'Catré'    #  →  false     roles.empty?   roles.clear  
  13. Types in Ruby roles  =  {  :LA  =>  'Gaspar',  :PM

     =>  'Tavares'  }   roles  =  {  LA:  'Gaspar',  PM:  'Tavares'  }   Alternate notation when keys are symbols RUBY  1.9.2  
  14. Operators ==   !=   <   >   <=

      >=   <=>   ===   .eql?   .equal?   Combined comparison  0  if first equals second  1  if first is greater than the second -­‐1  if first operand is less than the second
  15. Operators ==   !=   <   >   <=

      >=   <=>   ===   .eql?   .equal?   True if both have the same type and values 1.0  ==  1      #  true   1.0.eql?  1  #  false
  16. Operators ==   !=   <   >   <=

      >=   <=>   ===   .eql?   .equal?   True if both have the same object id a  =  1   b  =  1   a.equal?  b  #  true  (why?)
  17. Operators ==   !=   <   >   <=

      >=   <=>   ===   .eql?   .equal?   x  ?  y  :  z   Ternary operator
  18. Methods - The method name should be - If it’s it should

    end in a - If it it should end in an
  19. Methods - The method name should be - If it’s it should

    end in a - If it it should end in an
  20. Variables banana      #  is  a  variable   Banana

         #  is  a  constant   @banana    #  is  an  instance  variable   @@banana  #  is  a  class  variable   $banana    #  is  a  global  variable  
  21. Variables banana      #  is  a  variable   Banana

         #  is  a  constant   @banana    #  is  an  instance  variable   @@banana  #  is  a  class  variable   $banana    #  is  a  global  variable  
  22. Modules - You shouldn’t put things in a class that don’t

    really go together - Modules are a good excuse to break that rule without breaking it And because…
  23. Flow control if  something      puts  "It's  so  true."

      elsif  another_thing      puts  "Yeah!"   else      puts  "Wrong!"   end     puts  'Hello!'  if  something     redirect_to(home_path)  unless  current_user.logged_out?  
  24. Flow control case  http_status_code   when  404:  puts  "Not  found"

      when  400      puts  "Bad  Request"   else      puts  "Another  status  code"   end   case   when  http_status_code  <  200      puts  "Informational"   when  http_status_code.between?(200,  300)      puts  "Success!"   else      puts  "Maybe  next  time."   end  
  25. class  Sample      def  hello        

     5.times  {  print  "Hello  World!"  }      end   end     sample  =  Sample.new   sample.hello 0   1   2   3   4   5   6   7 Create a class with a method which prints 5 times the sentence “Hello World!”
  26. lines  =  File.new('file').readlines   puts  "Chars=%d,  Words=%d,  Lines=%d"  % [lines.join.size,

     lines.join.split.size,   lines.size] 0   1       Create a method which reads a text file and prints the number of characters, words and lines
  27. require  'net/http'   Net::HTTP.start('www.ruby-­‐lang.org',  80)  do  |http|      print

     http.get('/en/LICENSE.txt').body   end 0   1   2   3       Use Ruby to make an HTTP GET request on www.ruby-lang.org/en/LICENSE.txt
  28. 1  A Quick (and Hopefully Painless) Ride Through Ruby, viewed

    2011/03/31, http:// mislav.uniqpath.com/poignant-guide/book/chapter-3.html 2  Using Ruby - An Introduction to Ruby for Java Programmers, viewed 2011/03/31, http://onestepback.org/articles/usingruby/ 3  Ruby Operators, viewed 2011/03/31, http://www.tutorialspoint.com/ruby/ ruby_operators.htm 4  Mr. Neighborly's Humble Little Ruby Book, viewed 2011/03/31, 
 http://humblelittlerubybook.com/ 5  Ruby for Java programmers, viewed 2011/03/31, http://www.softwaresummit.com/ 2006/speakers/BowlerRubyForJavaProgrammers.pdf * [email protected] 31  March  2011  
  29. http://ruby.runpaint.org/ (draft of a book about Ruby 1.9) * [email protected]

    31  March  2011   http://humblelittlerubybook.com/ (book about Ruby 1.8) http://mislav.uniqpath.com/poignant-guide/ (guide featuring talking foxes) http://www.rubyist.net/~slagell/ruby/ (another guide)