SlideShare a Scribd company logo
*




     * splash@dei.uc.pt   31	
  March	
  2011	
  
This presentation is based on contents from
 Mr. Neighborly's Humble Little Ruby Book




                           * splash@dei.uc.pt   31	
  March	
  2011	
  
Chapter 0
def	
  what	
  is	
  ruby?	
  
	
  	
  'A	
  programming	
  language'	
  
end
def	
  why	
  do	
  i	
  need	
  to	
  learn	
  it?	
  
	
  	
  ruby	
  on	
  rails	
  =	
  'Ruby	
  language'	
  +	
  'Rails	
  framework'	
  
	
  	
  read	
  the	
  slides!	
  unless	
  u	
  likez	
  jsp	
  
end
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	
  
Ruby originated in Japan during the mid-1990s
Created by Yukihiro ‘Matz’ Matsumoto
Let’s try it out!
Let’s try it out!
Chapter 1
Basic concepts of Ruby



                                                     -­‐4.abs	
  	
  #	
  →	
  4	
  
                                                     6.zero?	
  #	
  →	
  false	
  




    if	
  (a	
  !=	
  null)	
  {...}	
     unless	
  a.nil?	
  {...}	
  
    null.toString()	
  //	
  NPE	
  	
     nil.to_s	
  #	
  ""	
  
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	
  
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	
  
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	
  
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	
  
Types in Ruby
Types in Ruby



                                                                         :hello	
  



  do_this	
  if	
  query	
  ==	
  :get	
  
  link_to	
  "View	
  Article",	
  :controller	
  =>	
  "articles",	
  :action	
  =>	
  "show"	
  
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	
  
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]	
  
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]	
  
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	
  
Types in Ruby



                                                      Alternate notation when
                                                      keys are symbols


   roles	
  =	
  {	
  :LA	
  =>	
  'Gaspar',	
  :PM	
  =>	
  'Tavares'	
  }	
  
   roles	
  =	
  {	
  LA:	
  'Gaspar',	
  PM:	
  'Tavares'	
  }	
  
                                                               RUBY	
  1.9.2	
  
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
Operators



    ==	
     !=	
     <	
        >	
         <=	
         >=	
          <=>	
  

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



    ==	
     !=	
     <	
        >	
      <=	
      >=	
     <=>	
  

   ===	
  

   .eql?	
  
                              True if both have
                              the same object id
   .equal?	
  

                              a	
  =	
  1	
  
                              b	
  =	
  1	
  
                              a.equal?	
  b	
  #	
  true	
  (why?)
Operators



    ==	
     !=	
     <	
     >	
     <=	
          >=	
       <=>	
  

   ===	
  

   .eql?	
                                     x	
  ?	
  y	
  :	
  z	
  
   .equal?	
                            Ternary operator
Operators



   &&	
      ||	
      !	
  
    =
    /        =
             /         =
   and	
     or	
     not	
  
Methods



  - The method name should be
  - If it’s                 it should end in a

  - If it                         it should end in an
Methods



  - The method name should be
  - If it’s                 it should end in a

  - If it                         it should end in an
Methods
Variables




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




            banana	
  	
  	
  #	
  is	
  a	
  variable	
  
            Banana	
  	
  	
  #	
  is	
  a	
  constant	
  
            @banana	
  	
  #	
  is	
  an	
  instance	
  variable	
  
            @@banana	
  #	
  is	
  a	
  class	
  variable	
  
            $banana	
  	
  #	
  is	
  a	
  global	
  variable	
  
Classes
Modules
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…
Modules

          mixins!
Blocks




  -  Somewhat similar to Java
     anonymous inner classes
Blocks
Blocks
Blocks
Closures

§  A closure is a combination of a                 and
    an




                                RUBY	
  1.9.2	
  
Closures

§  A closure is a combination of a              and
    an




                             RUBY	
  1.9.2	
  
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?	
  
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	
  
Flow control
Flow control
Chapter 2
String manipulation
Date & Time
Unit testing
Conclusions
Conclusions
Conclusions
Conclusions
Conclusions
Appendix
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
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]
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
	
  
* splash@dei.uc.pt   31	
  March	
  2011	
  
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




                                                     * splash@dei.uc.pt         31	
  March	
  2011	
  
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	
  
*




     * splash@dei.uc.pt   31	
  March	
  2011	
  

More Related Content

PDF
A Whirlwind Tour of Perl
PPTX
Php + my sql
PDF
Your code sucks, let's fix it! - php|tek13
PDF
Eloquent ruby
PPTX
Lecture4 php by okello erick
PPTX
Ruby @ Work
PDF
Learning Ruby with RubyWarrior
A Whirlwind Tour of Perl
Php + my sql
Your code sucks, let's fix it! - php|tek13
Eloquent ruby
Lecture4 php by okello erick
Ruby @ Work
Learning Ruby with RubyWarrior

Similar to Learning Ruby (20)

PDF
Ruby training day1
KEY
An introduction to Ruby
PPTX
Ruby data types and objects
PDF
Ruby Language - A quick tour
PPTX
Ruby from zero to hero
PPTX
Ruby -the wheel Technology
KEY
Introducing Ruby
PDF
Ruby 入門 第一次就上手
PDF
Slides chapter3part1 ruby-forjavaprogrammers
PDF
Kon nichi wa_ruby
PDF
Web Development With Ruby - From Simple To Complex
PDF
ruby1_6up
PDF
ruby1_6up
PDF
Ruby Intro {spection}
PDF
Ruby on Rails Presentation
PDF
RoR_2_Ruby
PDF
Ruby on Rails
PDF
Arulalan Ruby An Intro
PDF
Ruby — An introduction
Ruby training day1
An introduction to Ruby
Ruby data types and objects
Ruby Language - A quick tour
Ruby from zero to hero
Ruby -the wheel Technology
Introducing Ruby
Ruby 入門 第一次就上手
Slides chapter3part1 ruby-forjavaprogrammers
Kon nichi wa_ruby
Web Development With Ruby - From Simple To Complex
ruby1_6up
ruby1_6up
Ruby Intro {spection}
Ruby on Rails Presentation
RoR_2_Ruby
Ruby on Rails
Arulalan Ruby An Intro
Ruby — An introduction
Ad

Recently uploaded (20)

PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPT
Teaching material agriculture food technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Encapsulation theory and applications.pdf
PPTX
Cloud computing and distributed systems.
PDF
Electronic commerce courselecture one. Pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
MYSQL Presentation for SQL database connectivity
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Review of recent advances in non-invasive hemoglobin estimation
Diabetes mellitus diagnosis method based random forest with bat algorithm
Chapter 3 Spatial Domain Image Processing.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Teaching material agriculture food technology
Programs and apps: productivity, graphics, security and other tools
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
20250228 LYD VKU AI Blended-Learning.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Encapsulation theory and applications.pdf
Cloud computing and distributed systems.
Electronic commerce courselecture one. Pdf
Unlocking AI with Model Context Protocol (MCP)
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Ad

Learning Ruby

  • 1. * * splash@dei.uc.pt 31  March  2011  
  • 2. This presentation is based on contents from Mr. Neighborly's Humble Little Ruby Book * splash@dei.uc.pt 31  March  2011  
  • 4. def  what  is  ruby?      'A  programming  language'   end
  • 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. 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. Ruby originated in Japan during the mid-1990s Created by Yukihiro ‘Matz’ Matsumoto
  • 11. Basic concepts of Ruby -­‐4.abs    #  →  4   6.zero?  #  →  false   if  (a  !=  null)  {...}   unless  a.nil?  {...}   null.toString()  //  NPE     nil.to_s  #  ""  
  • 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. 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. 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. 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  
  • 17. Types in Ruby :hello   do_this  if  query  ==  :get   link_to  "View  Article",  :controller  =>  "articles",  :action  =>  "show"  
  • 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. 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. 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. 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. Types in Ruby Alternate notation when keys are symbols roles  =  {  :LA  =>  'Gaspar',  :PM  =>  'Tavares'  }   roles  =  {  LA:  'Gaspar',  PM:  'Tavares'  }   RUBY  1.9.2  
  • 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. Operators ==   !=   <   >   <=   >=   <=>   ===   True if both have the .eql?   same type and values .equal?   1.0  ==  1      #  true   1.0.eql?  1  #  false
  • 25. Operators ==   !=   <   >   <=   >=   <=>   ===   .eql?   True if both have the same object id .equal?   a  =  1   b  =  1   a.equal?  b  #  true  (why?)
  • 26. Operators ==   !=   <   >   <=   >=   <=>   ===   .eql?   x  ?  y  :  z   .equal?   Ternary operator
  • 27. Operators &&   ||   !   = / = / = and   or   not  
  • 28. Methods - The method name should be - If it’s it should end in a - If it it should end in an
  • 29. Methods - The method name should be - If it’s it should end in a - If it it should end in an
  • 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. Variables banana      #  is  a  variable   Banana      #  is  a  constant   @banana    #  is  an  instance  variable   @@banana  #  is  a  class  variable   $banana    #  is  a  global  variable  
  • 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. Modules mixins!
  • 37. Blocks -  Somewhat similar to Java anonymous inner classes
  • 41. Closures §  A closure is a combination of a and an RUBY  1.9.2  
  • 42. Closures §  A closure is a combination of a and an RUBY  1.9.2  
  • 43. 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?  
  • 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  
  • 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. 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. 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. * splash@dei.uc.pt 31  March  2011  
  • 61. 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 * splash@dei.uc.pt 31  March  2011  
  • 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. * * splash@dei.uc.pt 31  March  2011