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

Brief Ruby/Ruby on Rails intro

Brief Ruby/Ruby on Rails intro

Florian Plank

March 28, 2014
Tweet

More Decks by Florian Plank

Other Decks in Programming

Transcript

  1. RUBY
    A PROGRAMMER’S BEST FRIEND

    View Slide

  2. Background

    View Slide

  3. Yukihiro “Matz” Matsumoto

    View Slide

  4. I hope to see Ruby help every programmer in
    the world to be productive, and to enjoy
    programming, and to be happy. at is the
    primary purpose of Ruby language.”

    View Slide

  5. - Programmer happiness
    - Principle of least astonishment
    - Human readable
    - Beautiful syntax
    Principles

    View Slide

  6. - RubyGems, Bundler & Rake
    - Multiple implementations (MRI, JRuby,
    Rubinius, mruby, MacRuby, Topaz, …)
    - Solid Standard Library
    Ecosystem

    View Slide

  7. - MINASWAN
    - Self re ective
    - Open
    - Quirky
    Community

    View Slide

  8. The Language
    A Sales Pitch

    View Slide

  9. View Slide

  10. Object–oriented
    1

    View Slide

  11. 5.times { print "We love Ruby" }

    View Slide

  12. class Animal
    def eat(food)
    puts "Animal eating"
    end
    end
    my_animal = Animal.new
    animal.eat
    # => "Animal eating"

    View Slide

  13. class Dog < Animal
    def eat(food)
    puts "Dog eating"
    super
    end
    end

    View Slide

  14. module Stomach
    def digest(food)
    # ...
    end
    end

    View Slide

  15. class Dog < Animal
    include Stomach
    end
    my_dog = Dog.new
    dog.digest

    View Slide

  16. -199.abs
    # => 199
    "Foobar".split("").uniq.sort.join
    # => "abFor"
    nil.class
    # => "NilClass"

    View Slide

  17. Dynamically typed
    Duck–typing
    2

    View Slide

  18. if dog.is_a? Animal
    dog.eat
    end
    dog.eat if dog.respond_to?(:eat)

    View Slide

  19. Monkey–patching
    (Duck–punching)
    3

    View Slide

  20. … if it walks like a duck and talks like a
    duck, it’s a duck, right? So if this duck is not
    giving you the noise that you want, you’ve
    got to just punch that duck until it returns
    what you expect.”

    View Slide

  21. class String
    def yell
    "#{self.upcase}!"
    end
    end
    "hello".yell
    # => "HELLO!"

    View Slide

  22. Meta–programming
    4

    View Slide

  23. class Greeter
    def method_missing(name, *args)
    name = name.to_s
    if name =~ /^hello_/
    puts "Hello, #{name.gsub(/^hello_/, '')}!"
    else
    super
    end
    end
    end
    Greeter.new.hello_john
    # => "Hello, john!"

    View Slide

  24. Blocks & Lambdas
    5

    View Slide

  25. [1, 2, 3].map { |i| i ** 2 }
    # => [1, 4, 9]

    View Slide

  26. def greet(&block)
    # ...
    greeting = yield("John")
    # ...
    end
    greet do |name|
    "Hello, #{name}!"
    end

    View Slide

  27. A word on speed

    View Slide

  28. Ruby on Rails
    An (even shorter) sales pitch

    View Slide

  29. View Slide

  30. - Open Source Web Framework
    - MVC
    - Convention over Con guration
    - DRY
    - Opinionated
    Principles

    View Slide

  31. - Generators
    - ORM
    - Restful routing
    - Included webserver
    Features

    View Slide

  32. $ gem install rails
    $ rails new blog
    $ cd blog
    $ rails generate scaffold post title content:text
    $ rake db:migrate
    $ rails server

    View Slide

  33. author = Author.find_by :name => "John"
    author.posts.first.title

    View Slide

  34. Book.where(:title => 'Tale of Two Cities')
    .first_or_create

    View Slide

  35. class Account < ActiveRecord::Base
    # Returns all accounts with unread messages.
    def self.with_unread_messages
    joins(:messages).merge(Message.unread)
    end
    end

    View Slide

  36. View Slide