of 69 /69
Ruby & Rails Overview brought by Michal Poczwardowski and Gdansk 11/05/15

Ruby Rails Overview

  • Author
    netguru

  • View
    2.125

  • Download
    0

Embed Size (px)

Text of Ruby Rails Overview

  • Ruby & Rails Overviewbrought by Michal Poczwardowski and

    Gdansk 11/05/15

  • Michal PoczwardowskiRuby on Rails [email protected]

  • Software houseweb&mobile

  • Software houseweb&mobile

  • Agenda

    Part 1ruby

    Part 2rails

  • Part 1ruby

  • Ruby is a dynamic, scripting, object-oriented language...

  • Programming languages must feel natural to programmers.

    Yukihiro "Matz" Matsumoto

  • hello = Hello world!puts hello

    Hello world

  • Try ruby in a browser!

    tryruby.org

  • www.bloc.io/ruby-warrior

    Control brave knight using ruby

  • Why ruby is so cool?

  • #1: puts Yes if Work.starts_with? W

    #2: population = 12_000_000

    #3: misterious_number.between?(10, 20)

    Like a natural language

  • def really? trueend

    Aesthetic

  • 1: numbers = []2: for i in [1,2,3,4]3: numbers [1,4,9,16]

    Elegant - NON ruby-way solution

  • numbers = [1,2,3,4].map { |i| i ** 2 }

    numbers # => [1,4,9,16]

    Elegant - ruby-way solution

  • Everything is an object!

  • 42

  • 42.times { puts Awesome }

    Fixnum object

  • :001 > 1.class => Fixnum :002 > (2.2).class => Float :003 > [].class => Array :004 > "Politechnika Gdaska".class => String :005 > nil.class => NilClass :006 > {}.class => Hash

    Output from irb

  • Classes, objects01: class School02: attr_reader :name03: 04: def initialize(name)05: @name = name06: end07:08: def hello09: puts Hello #{name}10: end11: end

    school = School.new(PG)school.hello

    # => Hello PG

  • - high performance / lower level stuff- multi-threading- graphics / data analysis

    Avoid ruby in case of

  • Ruby is great at...

    Metaprogramming

  • Example with send01: class Rubyist02: def face(mood)03: send(mood)04: end05: 06: private07: 08: def happy09: :)10: end11:12: def sad13: :(14: end15: end

    dev = Rubyist.new

    dev.face(:happy)# => :)

    dev.face(:sad)# => :(

  • Handle missing methods

    1: class Rubyist2: def happy; :) end3: def sad; :( end4: 5: def method_missing(name)6: :?7: end8: end

    dev = Rubyist.new

    dev.happy# => :)

    dev.sad# => :(

    dev.excited# => :?

    dev.worried# => :?

  • Define own methods

    01: class Rubyist02: FACES = {03: happy: :),04: sad: :(,05: excited: ;D,06: angry: :[07: }08: 09: FACES.each do |key, value|10: define_method(key) { value }11: end12: end

    dev = Rubyist.new

    dev.happy# => :)

    dev.sad# => :(

    dev.angry# => :[

    dev.excited# => ;D

  • Everything changes

    1: class String2: def with_smile3: self + :)4: end5: end

    Sad string.with_smile# => Sad string :)

  • With great power comes great responsibility.Unkle Ben

  • Write tests!

  • Example rspecdescribe Rubyist do subject { described_class.new }

    describe #happy do it returns happy face expect(subject.happy).to eq :) end endend

  • library -> gem

  • rubygems.org/stats - 9/05/15

  • Gemfile01: source 'https://rubygems.org'02: 03: gem rails, 4.2.104: gem nokogiri05: gem 'stripe', git: 'https://github.com/stripe/stripe-ruby'06:07: group :test do08: gem rspec-rails09: end

  • Part 2ruby on rails

  • Rails is a web application development framework

  • Powerful web applications that formerly might have taken weeks or months to develop can be produced in

    a matter of days.Tim OReilly

  • Websites powered by Rails

  • isitrails.com

  • Convention over Configuration

  • Structure

  • controllers

    modelsviews

    routes.rb, database.yml

    Gemfile

  • MVC

  • controller

    model view

    browser

    DB

    routes

    web server

  • Lets prepare some code

  • $ rails generate model Post title:string content:text invoke active_record create db/migrate/20150509232514_create_posts.rb create app/models/post.rb invoke rspec create spec/models/post_spec.rb invoke factory_girl create spec/factories/posts.rb

    Magic spells

  • 01: class CreatePosts < ActiveRecord::Migration02: def change03: create_table :posts do |t|04: t.string :title05: t.text :content06: 07: t.timestamps08: end09: end10: end

    Migration

  • MVC in action

  • http://localhost:3000/

  • 127.0.0.1 - GET /index.html HTTP/1.0" 200 2326

  • get /, to: welcome#index

  • class WelcomeController < ApplicationController def index @posts = Post.all endend

  • class Post < ActiveRecord::Baseend

  • class Post < ActiveRecord::Baseend

  • class WelcomeController < ApplicationController def index @posts = Post.all endend

  • ERB

  • %ul - @posts do |post| %li =post.title

    HAML

  • http://localhost:3000/

  • This is almost the end...

  • Dont forget to visit netguru.co

    and ourbox no. 20

  • Thanks!