Ruby & Rails Overview
brought by Michal Poczwardowski and
Gdansk 11/05/15
Michal Poczwardowski
Ruby on Rails developer
michal.poczwardowski@netguru.co
Software house
web&mobile
Software house
web&mobile
Agenda
Part 1
ruby
Part 2
rails
Part 1
ruby
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?
true
end
Aesthetic
1: numbers = []
2: for i in [1,2,3,4]
3: numbers << i ** 2
4: end
5: 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 Gdańska".class
=> String
:005 > nil.class
=> NilClass
:006 > {}.class
=> Hash
Output from irb
Classes, objects
01: class School
02: attr_reader :name
03:
04: def initialize(name)
05: @name = name
06: end
07:
08: def hello
09: puts “Hello #{name}”
10: end
11: 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 send
01: class Rubyist
02: def face(mood)
03: send(mood)
04: end
05:
06: private
07:
08: def happy
09: ‘:)’
10: end
11:
12: def sad
13: ‘:(‘
14: end
15: end
dev = Rubyist.new
dev.face(:happy)
# => ‘:)’
dev.face(:sad)
# => ‘:(’
Handle missing methods
1: class Rubyist
2: def happy; ‘:)’ end
3: def sad; ‘:(‘ end
4:
5: def method_missing(name)
6: ‘:?’
7: end
8: end
dev = Rubyist.new
dev.happy
# => ‘:)’
dev.sad
# => ‘:(’
dev.excited
# => ‘:?’
dev.worried
# => ‘:?’
Define own methods
01: class Rubyist
02: FACES = {
03: happy: ‘:)’,
04: sad: ‘:(’,
05: excited: ‘;D’,
06: angry: ‘:[‘
07: }
08:
09: FACES.each do |key, value|
10: define_method(key) { value }
11: end
12: end
dev = Rubyist.new
dev.happy
# => ‘:)’
dev.sad
# => ‘:(’
dev.angry
# => ‘:[’
dev.excited
# => ‘;D’
Everything changes
1: class String
2: def with_smile
3: self + ‘ :)’
4: end
5: end
‘Sad string’.with_smile
# => ‘Sad string :)’
‘With great power comes great responsibility.’
Unkle Ben
Write tests!
Example rspec
describe Rubyist do
subject { described_class.new }
describe ‘#happy’ do
it ‘returns happy face’
expect(subject.happy).to eq ‘:)’
end
end
end
library -> gem
rubygems.org/stats - 9/05/15
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 do
08: gem ‘rspec-rails’
09: end
Part 2
ruby 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 O’Reilly
Websites powered by Rails
isitrails.com
Convention over
Configuration
Structure
controllers
models
views
routes.rb, database.yml
Gemfile
MVC
controller
model view
browser
DB
routes
web server
Let’s 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::Migration
02: def change
03: create_table :posts do |t|
04: t.string :title
05: t.text :content
06:
07: t.timestamps
08: end
09: end
10: 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
end
end
class Post < ActiveRecord::Base
end
class Post < ActiveRecord::Base
end
class WelcomeController < ApplicationController
def index
@posts = Post.all
end
end
<ul>
<% @posts.each do |post| %>
<li>
<%= post.title %>
</li>
<% end %>
</ul>
ERB
%ul
- @posts do |post|
%li
=post.title
HAML
<html>
…
<body>
…
<%= yield %>
…
</body>
</html>
http://localhost:3000/
This is almost the end...
Don’t forget to visit
netguru.co
and our
box no. 20
Thanks!

Ruby Rails Overview