Learning Ruby

1,711 views

Published on

Published in: Technology
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,711
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
73
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide

Learning Ruby

  1. 1. * * splash@dei.uc.pt 31  March  2011  
  2. 2. This presentation is based on contents from Mr. Neighborlys Humble Little Ruby Book * splash@dei.uc.pt 31  March  2011  
  3. 3. Chapter 0
  4. 4. def  what  is  ruby?      A  programming  language  end
  5. 5. def  why  do  i  need  to  learn  it?      ruby  on  rails  =  Ruby  language  +  Rails  framework      read  the  slides!  unless  u  likez  jsp  end
  6. 6. 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  
  7. 7. Ruby originated in Japan during the mid-1990sCreated by Yukihiro ‘Matz’ Matsumoto
  8. 8. Let’s try it out!
  9. 9. Let’s try it out!
  10. 10. Chapter 1
  11. 11. Basic concepts of Ruby -­‐4.abs    #  →  4   6.zero?  #  →  false   if  (a  !=  null)  {...}   unless  a.nil?  {...}   null.toString()  //  NPE     nil.to_s  #  ""  
  12. 12. 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  
  13. 13. 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  
  14. 14. Types in Ruby puts  hellotmiguel  #  →  hellotmiguel   puts  "hellotmiguel"  #  →  hello    miguel   -  Lightweight strings -  Escape sequences (t, n, …) -  Expression interpolation puts  "hello  #{manolo    *3}"   myvar.to_s   #  hello  manolo  manolo  manolo  
  15. 15. Types in Ruby %q{Text}  #  Single  quoted   %Q{Text}  #  Double  quoted   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  
  16. 16. Types in Ruby
  17. 17. Types in Ruby :hello   do_this  if  query  ==  :get   link_to  "View  Article",  :controller  =>  "articles",  :action  =>  "show"  
  18. 18. 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  
  19. 19. 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]  
  20. 20. 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]  
  21. 21. 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  
  22. 22. Types in Ruby Alternate notation when keys are symbols roles  =  {  :LA  =>  Gaspar,  :PM  =>  Tavares  }   roles  =  {  LA:  Gaspar,  PM:  Tavares  }   RUBY  1.9.2  
  23. 23. Operators ==   !=   <   >   <=   >=   <=>   ===   .eql?   Combined comparison  0  if first equals second  1  if first is greater than the second .equal?   -­‐1  if first operand is less than the second
  24. 24. Operators ==   !=   <   >   <=   >=   <=>   ===   True if both have the .eql?   same type and values .equal?   1.0  ==  1      #  true   1.0.eql?  1  #  false
  25. 25. Operators ==   !=   <   >   <=   >=   <=>   ===   .eql?   True if both have the same object id .equal?   a  =  1   b  =  1   a.equal?  b  #  true  (why?)
  26. 26. Operators ==   !=   <   >   <=   >=   <=>   ===   .eql?   x  ?  y  :  z   .equal?   Ternary operator
  27. 27. Operators &&   ||   !   = / = / = and   or   not  
  28. 28. Methods - The method name should be - If it’s it should end in a - If it it should end in an
  29. 29. Methods - The method name should be - If it’s it should end in a - If it it should end in an
  30. 30. Methods
  31. 31. Variables banana      #  is  a  variable   Banana      #  is  a  constant   @banana    #  is  an  instance  variable   @@banana  #  is  a  class  variable   $banana    #  is  a  global  variable  
  32. 32. Variables banana      #  is  a  variable   Banana      #  is  a  constant   @banana    #  is  an  instance  variable   @@banana  #  is  a  class  variable   $banana    #  is  a  global  variable  
  33. 33. Classes
  34. 34. Modules
  35. 35. 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…
  36. 36. Modules mixins!
  37. 37. Blocks -  Somewhat similar to Java anonymous inner classes
  38. 38. Blocks
  39. 39. Blocks
  40. 40. Blocks
  41. 41. Closures§  A closure is a combination of a and an RUBY  1.9.2  
  42. 42. Closures§  A closure is a combination of a and an RUBY  1.9.2  
  43. 43. Flow control if  something      puts  "Its  so  true."   elsif  another_thing      puts  "Yeah!"   else      puts  "Wrong!"   end     puts  Hello!  if  something     redirect_to(home_path)  unless  current_user.logged_out?  
  44. 44. Flow control case  http_status_code   case   when  404:  puts  "Not  found"   when  http_status_code  <  200   when  400      puts  "Informational"   when  http_status_code.between?(200,  300)      puts  "Bad  Request"      puts  "Success!"   else   else      puts  "Another  status  code"      puts  "Maybe  next  time."   end   end  
  45. 45. Flow control
  46. 46. Flow control
  47. 47. Chapter 2
  48. 48. String manipulation
  49. 49. Date & Time
  50. 50. Unit testing
  51. 51. Conclusions
  52. 52. Conclusions
  53. 53. Conclusions
  54. 54. Conclusions
  55. 55. Conclusions
  56. 56. Appendix
  57. 57. Create a class with a method which prints 5 times the sentence “Hello World!” 0   class  Sample  1      def  hello  2          5.times  {  print  "Hello  World!"  }  3      end  4   end  5    6   sample  =  Sample.new  7 sample.hello
  58. 58. Create a method which reads a text file and prints the number of characters, words and lines 0   lines  =  File.new(file).readlines  1   puts  "Chars=%d,  Words=%d,  Lines=%d"  %   [lines.join.size,  lines.join.split.size,   lines.size]
  59. 59. Use Ruby to make an HTTP GET request on www.ruby-lang.org/en/LICENSE.txt 0   require  net/http  1   Net::HTTP.start(www.ruby-­‐lang.org,  80)  do  |http|  2      print  http.get(/en/LICENSE.txt).body  3   end  
  60. 60. * splash@dei.uc.pt 31  March  2011  
  61. 61. 1  A Quick (and Hopefully Painless) Ride Through Ruby, viewed 2011/03/31, http:// mislav.uniqpath.com/poignant-guide/book/chapter-3.html2  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.htm4  Mr. Neighborlys 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 * splash@dei.uc.pt 31  March  2011  
  62. 62. http://ruby.runpaint.org/ (draft of a book about Ruby 1.9)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) * splash@dei.uc.pt 31  March  2011  
  63. 63. * * splash@dei.uc.pt 31  March  2011  

×