SlideShare une entreprise Scribd logo
Introduction au langage Ruby
Ruby is designed to make programmers happy
                      Matz (Yukihiro Matsumoto)
Perl + Python
Ruby =
           Smalltalk
Interprété
Interprété
Introspection
                   eval()

          REPL
Orienté Objet
   (Totalement)
Blocks
Blocks
[1,2,3].each do |item|
 puts item
end

file_contents = open(file_name) { |f| f.read }

widget.on_button_press { puts "Got Button Press" }
Blocks
def execute_n_times(times)
 (0...times).each do
  yield
 end
end

def execute_n_times_with_args(times, &block)
 (0...times).each do |time|
  block.call(time)
 end
end
messages != méthodes
messages != méthodes

  foo = "Bar"

  foo.length
  # Send :length to foo

  foo.index("a")
  # Send :index to foo with argument "a"

  1+2
  # Send :+ to object 1 with argument 2

  a = Array.new
  a[i]
  # Send :[] to a with argument i
messages != méthodes
class VCR

 def initialize
  @messages = []
 end

 # Captures everything
 def method_missing(method, *args, &block)
  @messages << [method, args, block]
 end

 # Replays all captured messages
 def play_back_to(obj)
  @messages.each do |method, args, block|
   obj.send(method, *args, &block)
  end
 end

end
messages != méthodes
Remote proxies
                   Auto loaders

      Decorators


                   Mock Objects
  Builders
Dynamique
Dynamique
         Modifier des classes existantes
class Integer

 def even?
  (self % 2) == 0
 end

end


class String

 def url_friendly
  self.downcase.gsub(/.../,'-').gsub(/-{2,}/,'-')
 end

end
Dynamique
            Méthodes singletons

class Dog
end

a_dog = Dog.new
anoher_dog = Dog.new

def a_dog.bark
 "Waff Waff"
end

a_dog.bark # => "Waff Waff"
another_dog.bark # => NoMethodError
Dynamique
                           Hooks

class Tracing

 def self.method_added(name)
  puts "Method #{name} added to #{self.name} class."
 end

 def new_method
  # Yada yada yada
 end
end
Dynamique
                            Code Eval
class Module
 def trace_attr(sym)
   self.module_eval %{
    def #{sym}
      printf "Accessing %s with value %sn",
        "#{sym}",
        @#{sym}.inspect

     @#{sym}
    end
  }
 end
end

class Dog
 trace_attr :name

 def initialize(dog_name)
  @name = dog_name
 end
end

Dog.new("Fido").name
# => Accessing name with value "Fido"
Introduction au langage Ruby
When I see a bird
 - that walks like a duck
 - and swims like a duck
 - and quacks like a duck,

I call that bird a duck.
                           James Whitcomb Riley
Pas besoin dʼinterface !


class Duck
 def talk; puts "Quack"; end
end

class DuckLikeObject
 def talk; puts "Kwak"; end
end

flock = [ Duck.new, DuckLikeObject.new ]

flock.each do |d|
 d.talk
end
Introduction au langage Ruby
DSL
DSL
               (Rake)
desc "Clean active targets"
task :clean do
 # ...
end

desc "Build the project"
task :build => :clean do
 # ...
end

task :default => :build
desc "Clean active targets"
task :clean do
 # ...
end

desc "Build the project"
task :build => :clean do
 # ...
end

task :default => :build




     def desc(description)
      # ...
     end

     def task(*args, &block)
      # ...
      block.call
     end
Have Fun !
http://creativecommons.org/licenses/by-nc-sa/2.0/fr/


Vous êtes libres de reproduire, distribuer et communiquer cette création au
                 public, ainsi que de modifier cette création



            Paternité. Vous devez citer le nom de l'auteur original de la manière indiquée par l'auteur de
            l'oeuvre ou le titulaire des droits qui vous confère cette autorisation (mais pas d'une manière qui
            suggérerait qu'ils vous soutiennent ou approuvent votre utilisation de l'oeuvre).



            Pas d'Utilisation Commerciale.Vous n'avez pas le droit d'utiliser cette création à des fins
            commerciales.




            Partage des Conditions Initiales à l'Identique. Si vous modifiez, transformez ou adaptez
            cette création, vous n'avez le droit de distribuer la création qui en résulte que sous un contrat
            identique à celui-ci.

Contenu connexe

PPTX
HumanTalk - Commentaires, je t'aime un peu, beaucoup, à la folie, pas du tout
KEY
Pourquoi Ruby et Ruby On Rais sont geniaux ?
PDF
Meet up symfony 11 octobre 2016 - Les formulaire
PPT
PHP - fonctions mysql
PDF
PHP 1 - Apprendre les bases
PPT
Cours php
PPTX
Uniform Variable Syntax
PDF
Javascript #2 : valeurs, types & opérateurs
HumanTalk - Commentaires, je t'aime un peu, beaucoup, à la folie, pas du tout
Pourquoi Ruby et Ruby On Rais sont geniaux ?
Meet up symfony 11 octobre 2016 - Les formulaire
PHP - fonctions mysql
PHP 1 - Apprendre les bases
Cours php
Uniform Variable Syntax
Javascript #2 : valeurs, types & opérateurs

Tendances (11)

PPTX
Introduction à Ruby 2
PPTX
Introduction à Ruby - MS Techdays 2012
PDF
Programmer le robot humanoïde NAO [French]
PDF
jQuery — fonctionnalités avancées
PDF
Code Week 2014 - atelier d'initiation à la programmation avec python
PPTX
Breizh camp intro ruby
ODP
Tp-jquery
PPTX
Formation python 3
PDF
Playing With PHP 5.3
PPT
chapitre1.ppt
Introduction à Ruby 2
Introduction à Ruby - MS Techdays 2012
Programmer le robot humanoïde NAO [French]
jQuery — fonctionnalités avancées
Code Week 2014 - atelier d'initiation à la programmation avec python
Breizh camp intro ruby
Tp-jquery
Formation python 3
Playing With PHP 5.3
chapitre1.ppt
Publicité

Similaire à Introduction au langage Ruby (6)

ODP
Ruby Pour RoR
PPTX
Scriptez en Ruby by louis kottman
PDF
Ruby
PDF
Quelle place pour le framework Rails dans le développement d'application web
KEY
Présentation de Ruby on Rails
PDF
Introduction à Ruby
Ruby Pour RoR
Scriptez en Ruby by louis kottman
Ruby
Quelle place pour le framework Rails dans le développement d'application web
Présentation de Ruby on Rails
Introduction à Ruby
Publicité

Dernier (7)

PDF
FORMATION EN Programmation En Langage C.pdf
PDF
Tendances tech 2025 - SFEIR & WENVISION.pdf
PDF
FORMATION COMPLETE EN EXCEL DONE BY MR. NYONGA BRICE.pdf
PPTX
Souveraineté numérique - Définition et enjeux pour les entreprises et les dév...
PDF
presentation_with_intro_compressee IEEE EPS France
PDF
Modems expliqués- votre passerelle vers Internet.pdf
PPTX
Presentation_Securite_Reseaux_Bac+2.pptx
FORMATION EN Programmation En Langage C.pdf
Tendances tech 2025 - SFEIR & WENVISION.pdf
FORMATION COMPLETE EN EXCEL DONE BY MR. NYONGA BRICE.pdf
Souveraineté numérique - Définition et enjeux pour les entreprises et les dév...
presentation_with_intro_compressee IEEE EPS France
Modems expliqués- votre passerelle vers Internet.pdf
Presentation_Securite_Reseaux_Bac+2.pptx

Introduction au langage Ruby

  • 2. Ruby is designed to make programmers happy Matz (Yukihiro Matsumoto)
  • 3. Perl + Python Ruby = Smalltalk
  • 6. Orienté Objet (Totalement)
  • 8. Blocks [1,2,3].each do |item| puts item end file_contents = open(file_name) { |f| f.read } widget.on_button_press { puts "Got Button Press" }
  • 9. Blocks def execute_n_times(times) (0...times).each do yield end end def execute_n_times_with_args(times, &block) (0...times).each do |time| block.call(time) end end
  • 11. messages != méthodes foo = "Bar" foo.length # Send :length to foo foo.index("a") # Send :index to foo with argument "a" 1+2 # Send :+ to object 1 with argument 2 a = Array.new a[i] # Send :[] to a with argument i
  • 12. messages != méthodes class VCR def initialize @messages = [] end # Captures everything def method_missing(method, *args, &block) @messages << [method, args, block] end # Replays all captured messages def play_back_to(obj) @messages.each do |method, args, block| obj.send(method, *args, &block) end end end
  • 13. messages != méthodes Remote proxies Auto loaders Decorators Mock Objects Builders
  • 15. Dynamique Modifier des classes existantes class Integer def even? (self % 2) == 0 end end class String def url_friendly self.downcase.gsub(/.../,'-').gsub(/-{2,}/,'-') end end
  • 16. Dynamique Méthodes singletons class Dog end a_dog = Dog.new anoher_dog = Dog.new def a_dog.bark "Waff Waff" end a_dog.bark # => "Waff Waff" another_dog.bark # => NoMethodError
  • 17. Dynamique Hooks class Tracing def self.method_added(name) puts "Method #{name} added to #{self.name} class." end def new_method # Yada yada yada end end
  • 18. Dynamique Code Eval class Module def trace_attr(sym) self.module_eval %{ def #{sym} printf "Accessing %s with value %sn", "#{sym}", @#{sym}.inspect @#{sym} end } end end class Dog trace_attr :name def initialize(dog_name) @name = dog_name end end Dog.new("Fido").name # => Accessing name with value "Fido"
  • 20. When I see a bird - that walks like a duck - and swims like a duck - and quacks like a duck, I call that bird a duck. James Whitcomb Riley
  • 21. Pas besoin dʼinterface ! class Duck def talk; puts "Quack"; end end class DuckLikeObject def talk; puts "Kwak"; end end flock = [ Duck.new, DuckLikeObject.new ] flock.each do |d| d.talk end
  • 23. DSL
  • 24. DSL (Rake) desc "Clean active targets" task :clean do # ... end desc "Build the project" task :build => :clean do # ... end task :default => :build
  • 25. desc "Clean active targets" task :clean do # ... end desc "Build the project" task :build => :clean do # ... end task :default => :build def desc(description) # ... end def task(*args, &block) # ... block.call end
  • 27. http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Vous êtes libres de reproduire, distribuer et communiquer cette création au public, ainsi que de modifier cette création Paternité. Vous devez citer le nom de l'auteur original de la manière indiquée par l'auteur de l'oeuvre ou le titulaire des droits qui vous confère cette autorisation (mais pas d'une manière qui suggérerait qu'ils vous soutiennent ou approuvent votre utilisation de l'oeuvre). Pas d'Utilisation Commerciale.Vous n'avez pas le droit d'utiliser cette création à des fins commerciales. Partage des Conditions Initiales à l'Identique. Si vous modifiez, transformez ou adaptez cette création, vous n'avez le droit de distribuer la création qui en résulte que sous un contrat identique à celui-ci.

Notes de l'éditeur

  • #7: Lancer irb
  • #10: require &amp;#xAB;blocks&amp;#xBB;
  • #13: require &amp;#xAB;vcr&amp;#xBB;
  • #14: b = Builder::XmlMarkup.new
  • #16: require &amp;#x2018;extensions
  • #18: require &amp;#x2018;tracing&amp;#x2019;
  • #19: require &amp;#x2018;module_tracing&amp;#x2019;
  • #22: require &amp;#x2018;duck_typing&amp;#x2019;
  • #23: exemple geolocalisation require &amp;#x2018;mixins&amp;#x2019;