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

Introduction à Ruby

Introduction à Ruby

Présentation donnée le 23 février 2012 au Web à Québec 2012 (Québec).

Rémi Prévost

February 23, 2012
Tweet

More Decks by Rémi Prévost

Other Decks in Technology

Transcript

  1. # Fixnum 7 # String "foo" # Array [4, 7,

    15, 16, 23, 42] # Hash { :foo => "bar", :omg => 3 }
  2. # Symbol :foo # Regex /[a-z]{0,10}\s+$/i # Proc lambda {

    |args| return "hello" } # NilClass nil
  3. puts "Hello world." # puts("Hello world.") puts :foo => :bar,

    :omg => 3 # puts({ :foo => :bar, :omg => 3 })
  4. def hello(name) another_method("foo") return "Hello, #{name.capitalize}" end # ou def

    hello(name) another_method("foo") "Hello, #{name.capitalize}" end hello("rémi") => "Hello, Rémi"
  5. [1, 2, 3].map { |x| x * 2 } #

    ou [1, 2, 3].map do |x| x * 2 end => [2, 4, 6]
  6. "foo".upcase => "FOO" 108.to_s => "108" [1, 2, 3, 4].shuffle

    => [2, 4, 1, 3] { :foo => "bar", :omg => 3 }.keys => [:foo, :omg]
  7. 2 + 3 # ou # 2.+(3) => 5 [1,

    2, 3] << 4 # ou # [1, 2, 3].<<(4) => [1, 2, 3, 4]
  8. class Article < Contenu attr_reader :published def initialize(published=true) @published =

    published end def publish! @published = true end end article = Article.new(false) article.publish! article.published => true
  9. class Article def delete puts "Suppression de l’article!" end end

    class Article def delete puts "Finalement, on ne fait rien!" end end article = Article.new article.delete => "Finalement, on ne fait rien!"
  10. class Article def delete puts "Suppression de l’article!" end end

    article = Article.new class Article def delete puts "Finalement, on ne fait rien!" end end article.delete => "Finalement, on ne fait rien!"
  11. class Fixnum def minutes self * 60 end def hours

    self * 60.minutes end end 2.minutes => 120 5.hours => 18000
  12. class Object def lol! "LOL!" end end 1.lol! => "LOL!"

    "Test".lol! => "LOL!" [1, 2, 3].lol! => "LOL!"
  13. module SuperModule def super! "Hé, #{self}, super!" end end class

    String include SuperModule end "Web à Québec".super! => "Hé, Web à Québec, super!"
  14. $ ls /var/www/mon-application-php wp-atom.php wp-feed.php index.php wp-blog-header.php wp-includes/ license.txt wp-comments-post.php

    wp-opml.php wp-login.php wp-commentsrss2.php wp-load.php wp-pass.php readme.html wp-config.php wp-rdf.php wp-activate.php wp-mail.php wp-admin/ wp-content/ wp-app.php wp-cron.php $ ls /var/www/mon-application-ruby Gemfile Gemfile.lock config.ru app.rb
  15. $ gem install httparty Fetching: multi_xml-0.4.1.gem (100%) Fetching: httparty-0.8.1.gem (100%)

    When you HTTParty, you must party hard! Successfully installed multi_xml-0.4.1 Successfully installed httparty-0.8.1 2 gems installed $ irb >> HTTParty NameError: uninitialized constant Object::HTTParty >> require "httparty" => true >> HTTParty => HTTParty
  16. $ bundle --version command not found: bundle $ gem install

    bundler Successfully installed bundler-1.0.22 1 gem installed $ bundle --version Bundler version 1.0.22
  17. DHH

  18. $ tree . . !"" a-propos.rb !"" index.rb #"" projets.rb

    0 directories, 3 files $ curl http://localhost/index.rb Ma page d’accueil! $ curl http://localhost/a-propos.rb Moi, bla bla bla. NOPE
  19. # Contenu de config.ru class Blogue def call(env) headers =

    { "Content-type" => "text/html" } [200, headers, "Hello world."] end end run Blogue.new
  20. $ gem install rack $ rackup config.ru WEBrick 1.3.1 ruby

    1.9.2 (2011-07-09) WEBrick::HTTPServer#start: pid=69232 port=9292 $ curl http://localhost:9292 Hello world. $ curl http://localhost:9292/foo/bar Hello world.
  21. $ rails new mon_application … $ du --max-depth 1 -h

    . 28K ./app 60K ./config 4,0K ./db 4,0K ./doc 0 ./lib 0 ./log 24K ./public 4,0K ./script 8,0K ./test 0 ./tmp 0 ./vendor 164K .
  22. # Contenu de Gemfile source "http://rubygems.org" gem "sinatra" gem "dm-core"

    gem "dm-mysql-adapter" gem "dm-migrations" gem "haml"
  23. # Contenu de Gemfile source "http://rubygems.org" gem "sinatra", "~> 1.3"

    gem "dm-core" gem "dm-mysql-adapter" gem "dm-migrations" gem "haml", "3.1"
  24. $ bundle install => Fetching gem metadata from http://rubygems.org/ Installing

    addressable (…) Installing data_objects (…) Installing dm-core (…) Installing dm-mysql-adapter (…) Installing dm-migrations (…) Installing rack (…) Installing tilt (…) Installing sinatra (…) Installing haml (…) Using bundler (…) Your bundle is complete!
  25. # Contenu de blogue.rb configure do DataMapper::Logger.new(STDOUT, :debug) DataMapper.setup(:default, "mysql://localhost/blogue")

    end class Article include DataMapper::Resource # Un module! property :id, Serial property :title, String property :content, Text property :created_at, DateTime end DataMapper.finalize DataMapper.auto_upgrade!
  26. Article.all => [#<Article @id=1>, #<Article @id=2>, …] Article.all(:created_at.gte => Time.now

    - 3600) => [#<Article @id=3>] Article.get(42) => #<Article @id=42> Article.create(:title => "Hello!") => #<Article @id=1> Article.all(:title => "Un test").destroy => true
  27. get "/" do … end post "/articles" do … end

    put "/articles/:id" do … end delete "/articles/:id" do … end
  28. get "/articles/:id" do # params[:id] end get /\/articles\/(?<id>[0-9]+)/ do #

    params[:id] end get "/articles/*" do # params[:splat] end get "/articles/:id.?:format?" do # params[:id], params[:format] end
  29. get "/" do @articles = Article.all end get "/articles/:id" do

    @article = Article.get(params[:id]) end
  30. get "/" do @articles = Article.all end get "/articles/:id" do

    @article = Article.find(params[:id]) end
  31. get "/" do @articles = Article.all "Il y a #{@articles.length}

    articles." end get "/articles/:id" do @article = Article.get(params[:id]) "C’est bien #{@article.title}." end
  32. get "/" do @articles = Article.all haml :index end get

    "/articles/:id" do @article = Article.get(params[:id]) haml :show end
  33. %h1 Hello world. %p Voici une liste : %ul %li

    %a{ :href => "http://exomel.com" } Rémi <h1>Hello world.</h1> <p>Voici une liste :</p> <ul> <li> <a href="http://exomel.com">Rémi</a> </li> </ul>
  34. get "/" do @articles = Article.all haml :index, :layout =>

    :blogue end get "/articles/:id" do @article = Article.get(params[:id]) haml :show, :layout => :blogue end
  35. -# Exemple de http://localhost/articles/1 <!doctype html> <title>Mon blogue</title> <body> <h1>Lorem

    Ipsum</h1> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla dignissim, libero quis dapibus scelerisque
  36. # Contenu de blogue.rb configure do DataMapper::Logger.new STDOUT, :debug DataMapper.setup

    :default, "mysql://localhost/blogue" class Article include DataMapper::Resource property :id, Serial property :title, String property :content, Text property :created_at, DateTime end DataMapper.finalize DataMapper.auto_upgrade! end
  37. # Suite de blogue.rb get "/" do @articles = Article.all

    haml :index, :layout => :blogue end get "/articles/:id" do @article = Article.get(params[:id]) haml :show, :layout => :blogue end post "/articles" do @article = Article.create(params[:article]) redirect to("/article/#{@article.id}") end
  38. -# Contenu de views/index.haml %ul - @articles.each do |article| %li

    %h1= article.title = article.content -# Contenu de views/show.haml %h1= @article.title = @article.content -# Contenu de views/blogue.haml !!! 5 %title Mon blogue %body = yield
  39. configure do DataMapper::Logger.new STDOUT, :debug DataMapper.setup :default, "mysql://localhost/blogue" class Article

    include DataMapper::Resource property :id, Serial property :title, String property :content, Text property :created_at, DateTime end DataMapper.finalize DataMapper.auto_upgrade! end get "/" do @articles = Article.all haml :index, :layout => :blogue end get "/articles/:id" do @article = Article.find(params[:id]) haml :show, :layout => :blogue end post "/articles" do @article = Article.create(params[:article]) redirect to("/article/#{@article.id}") end
  40. $ gem install heroku $ heroku apps:create blogue Creating blogue...

    done Git remote heroku added $ git push heroku master -----> Heroku receiving push -----> Ruby/Rack app detected -----> Gemfile detected, running Bundler version 1.0.7 Unresolved dependencies detected; Installing... Your bundle is complete! -----> Launching... done, v1 http://blogue.heroku.com deployed to Heroku
  41. $ sudo gem install passenger $ sudo passenger-install-apache2-module LoadModule passenger_module

    /usr/lib/ruby/gems/ 1.9.1/gems/passenger-3.0.11/ext/apache2/ mod_passenger.so PassengerRoot /usr/lib/ruby/gems/1.9.1/gems/ passenger-3.0.11 PassengerRuby /usr/bin/ruby1.9.1
  42. <VirtualHost *:80> ServerName blogue.webaquebec.org DocumentRoot /var/www/blogue/public <Directory /var/www/blogue/public> Allow from

    all Options -MultiViews </Directory> </VirtualHost> $ cd /var/www/blogue $ mkdir public $ mkdir tmp $ sudo apachectl restart
  43. $ sudo gem install sinatra $ cat config.ru require "sinatra"

    get "/" do "Hello world." end run Sinatra::Application $ rackup config.ru WEBrick 1.3.1 ruby 1.9.2 (2011-07-09) WEBrick::HTTPServer#start: pid=69232 port=9292