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

Ruby Rails Overview

Jul 18, 2015

Download

Software

netguru
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Ruby Rails Overview

Ruby & Rails Overviewbrought by Michal Poczwardowski and

Gdansk 11/05/15

Page 2: Ruby Rails Overview

Michal PoczwardowskiRuby on Rails [email protected]

Page 3: Ruby Rails Overview

Software houseweb&mobile

Page 4: Ruby Rails Overview

Software houseweb&mobile

Page 5: Ruby Rails Overview

Agenda

Part 1

ruby

Part 2

rails

Page 6: Ruby Rails Overview

Part 1

ruby

Page 7: Ruby Rails Overview

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

Page 8: Ruby Rails Overview

‘Programming languages must feel natural to programmers.’

Yukihiro "Matz" Matsumoto

Page 9: Ruby Rails Overview
Page 10: Ruby Rails Overview

hello = ‘Hello world!’puts hello

Hello world

Page 11: Ruby Rails Overview

Try ruby in a browser!

tryruby.org

Page 12: Ruby Rails Overview
Page 13: Ruby Rails Overview

www.bloc.io/ruby-warrior

Control brave knight using ruby

Page 14: Ruby Rails Overview
Page 15: Ruby Rails Overview

Why ruby is so cool?

Page 16: Ruby Rails Overview

#1: puts ‘Yes’ if ‘Work’.starts_with? ‘W’

#2: population = 12_000_000

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

Like a natural language

Page 17: Ruby Rails Overview

def really? trueend

Aesthetic

Page 18: Ruby Rails Overview

1: numbers = []2: for i in [1,2,3,4]3: numbers << i ** 24: end5: numbers # => [1,4,9,16]

Elegant - NON ruby-way solution

Page 19: Ruby Rails Overview

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

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

Elegant - ruby-way solution

Page 20: Ruby Rails Overview

Everything is an object!

Page 21: Ruby Rails Overview

42

Page 22: Ruby Rails Overview

42.times { puts ‘Awesome’ }

Fixnum object

Page 23: Ruby Rails Overview

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

Output from irb

Page 24: Ruby Rails Overview

Classes, objects

01: 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’

Page 25: Ruby Rails Overview

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

Avoid ruby in case of

Page 26: Ruby Rails Overview

Ruby is great at...

Metaprogramming

Page 27: Ruby Rails Overview

Example with send

01: 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)# => ‘:(’

Page 28: Ruby Rails Overview

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# => ‘:?’

Page 29: Ruby Rails Overview

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’

Page 30: Ruby Rails Overview

Everything changes

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

‘Sad string’.with_smile# => ‘Sad string :)’

Page 31: Ruby Rails Overview

‘With great power comes great responsibility.’Unkle Ben

Page 32: Ruby Rails Overview

Write tests!

Page 33: Ruby Rails Overview

Example rspec

describe Rubyist do subject { described_class.new }

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

Page 34: Ruby Rails Overview

library -> gem

Page 35: Ruby Rails Overview
Page 36: Ruby Rails Overview

rubygems.org/stats - 9/05/15

Page 37: Ruby Rails Overview

Gemfile

01: source 'https://rubygems.org'02: 03: gem ‘rails’, ‘4.2.1’04: gem ‘nokogiri’05: gem 'stripe', git: 'https://github.com/stripe/stripe-ruby'06:07: group :test do08: gem ‘rspec-rails’09: end

Page 38: Ruby Rails Overview

Part 2

ruby on rails

Page 39: Ruby Rails Overview

Rails is a web application development framework

Page 40: Ruby Rails Overview

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

a matter of days.’Tim O’Reilly

Page 41: Ruby Rails Overview

Websites powered by Rails

Page 42: Ruby Rails Overview
Page 43: Ruby Rails Overview
Page 44: Ruby Rails Overview
Page 45: Ruby Rails Overview

isitrails.com

Page 46: Ruby Rails Overview

Convention over Configuration

Page 47: Ruby Rails Overview

Structure

Page 48: Ruby Rails Overview

controllers

modelsviews

routes.rb, database.yml

Gemfile

Page 49: Ruby Rails Overview

MVC

Page 50: Ruby Rails Overview

controller

model view

browser

DB

routes

web server

Page 51: Ruby Rails Overview

Let’s prepare some code

Page 52: Ruby Rails Overview

$ 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

Page 53: Ruby Rails Overview

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

Page 54: Ruby Rails Overview

MVC in action

Page 55: Ruby Rails Overview

http://localhost:3000/

Page 56: Ruby Rails Overview

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

Page 57: Ruby Rails Overview

get ‘/’, to: ‘welcome#index’

Page 58: Ruby Rails Overview

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

Page 59: Ruby Rails Overview

class Post < ActiveRecord::Baseend

Page 60: Ruby Rails Overview
Page 61: Ruby Rails Overview

class Post < ActiveRecord::Baseend

Page 62: Ruby Rails Overview

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

Page 63: Ruby Rails Overview

<ul> <% @posts.each do |post| %> <li> <%= post.title %> </li> <% end %></ul>

ERB

Page 64: Ruby Rails Overview

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

HAML

Page 65: Ruby Rails Overview

<html> … <body> … <%= yield %> … </body></html>

Page 66: Ruby Rails Overview

http://localhost:3000/

Page 67: Ruby Rails Overview

This is almost the end...

Page 68: Ruby Rails Overview

Don’t forget to visit netguru.co

and ourbox no. 20

Page 69: Ruby Rails Overview

Thanks!