SlideShare ist ein Scribd-Unternehmen logo
1 von 63
Downloaden Sie, um offline zu lesen
*




     * 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	
  

Weitere ähnliche Inhalte

Ähnlich wie Learning Ruby

Ruby from zero to hero
Ruby from zero to heroRuby from zero to hero
Ruby from zero to heroDiego Lemos
 
Intro to Ruby/Rails at TechLady Hackathon
Intro to Ruby/Rails at TechLady HackathonIntro to Ruby/Rails at TechLady Hackathon
Intro to Ruby/Rails at TechLady Hackathonkdmcclin
 
Ruby 程式語言綜覽簡介
Ruby 程式語言綜覽簡介Ruby 程式語言綜覽簡介
Ruby 程式語言綜覽簡介Wen-Tien Chang
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentationadamcookeuk
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technologyppparthpatel123
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perlworr1244
 
Rails Is From Mars Ruby Is From Venus Presentation 1
Rails Is From Mars  Ruby Is From Venus Presentation 1Rails Is From Mars  Ruby Is From Venus Presentation 1
Rails Is From Mars Ruby Is From Venus Presentation 1railsconf
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Aslak Hellesøy
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Aslak Hellesøy
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Aslak Hellesøy
 
Code Fast, die() Early, Throw Structured Exceptions
Code Fast, die() Early, Throw Structured ExceptionsCode Fast, die() Early, Throw Structured Exceptions
Code Fast, die() Early, Throw Structured ExceptionsJohn Anderson
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation TutorialLorna Mitchell
 
RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby Gautam Rege
 

Ähnlich wie Learning Ruby (20)

Ruby from zero to hero
Ruby from zero to heroRuby from zero to hero
Ruby from zero to hero
 
Intro to Ruby/Rails at TechLady Hackathon
Intro to Ruby/Rails at TechLady HackathonIntro to Ruby/Rails at TechLady Hackathon
Intro to Ruby/Rails at TechLady Hackathon
 
Ruby Gotchas
Ruby GotchasRuby Gotchas
Ruby Gotchas
 
Ruby Style Guide
Ruby Style GuideRuby Style Guide
Ruby Style Guide
 
Ruby Gotchas
Ruby GotchasRuby Gotchas
Ruby Gotchas
 
Ruby 程式語言綜覽簡介
Ruby 程式語言綜覽簡介Ruby 程式語言綜覽簡介
Ruby 程式語言綜覽簡介
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentation
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technology
 
Rails by example
Rails by exampleRails by example
Rails by example
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Rails Is From Mars Ruby Is From Venus Presentation 1
Rails Is From Mars  Ruby Is From Venus Presentation 1Rails Is From Mars  Ruby Is From Venus Presentation 1
Rails Is From Mars Ruby Is From Venus Presentation 1
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
 
Code Fast, die() Early, Throw Structured Exceptions
Code Fast, die() Early, Throw Structured ExceptionsCode Fast, die() Early, Throw Structured Exceptions
Code Fast, die() Early, Throw Structured Exceptions
 
Ruby
RubyRuby
Ruby
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
 
Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
 
Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
 
RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby
 

Kürzlich hochgeladen

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 

Kürzlich hochgeladen (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

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