Everyday Rails
brought by Blaze Hadzik and
Blaze Hadzik
Ruby on Rails developer
blazej.hadzik@netguru.co
netguru
Software house
web&mobile
Team
Poznan, Warsaw, Gdansk, Krakow
Australia, India, America, Brazil
#transparency
Workshops
2 days
teams with mentors
fun
recruitment
/ˈruː.bi/
‘Programming languages must feel
natural to programmers.’
Matz
Ruby is a dynamic, scripting, object-
oriented language...
:014 > 1.class
=> Fixnum
:015 > (2.2).class
=> Float
:016 > [].class
=> Array
:017 > "Politechnika Slaska".class
=> String
:018 > nil.class
=> NilClass
:019 > “abc” + “d”
=> “abcd”
variables
type local instance class global constant
example name @name @@name $name NAME
you don’t have to specify variable type
Variables
a = 12
a.class # => Integer
a = “polsl”
a.class # => String
a = [‘a’, ‘b’, ‘c’]
a.class # => Array
Arrays and Hashes
a = [ 'ant', 'bee', 'cat', 'dog', 'elk' ]
a[0] # => "ant"
a[3] # => "dog"
# this is the same:
a = %w{ ant bee cat dog elk }
a[0] # => "ant"
a[3] # => "dog"
Arrays and Hashes
my_hash = {
building: ‘school’,
fruit: ‘orange’
}
puts my_hash[:building] # => ‘school’
Symbols
LOW_PRIORITY = 0
HIGH_PRIORITY = 1
priority = HIGH_PRIORITY
vs
priority = :high
Symbols are simply constants that you don’t have to predeclare and that are guaranteed to be unique.
Control structures
if/else statements
case statements
while structure
Blocks
Idioms
Idioms
a, b = 1, 2
a += b
a # => 3
@a ||= 1 # @a = 1 if @a.nil?
Classes, objects
Classes, objects
class School
def initialize(name)
@name = name
end
end
school = School.new(‘polsl’)
p school # =>#<School:0x007fa301836160 @name="polsl">
Class Attributes
class School
def initialize(name)
@name = name
end
def name=(name)
@name = name
end
def name
@name
end
end
Class Attributes
attr_reader
attr_writer
attr_accessor
Access Control
private
protected
public
Rails
‘It is impossible not to notice Ruby on Rails.’
Martin Fowler
Ruby is language
Rails is framework
DRY
Service Objects
Decorators
Convention over
Configuration
REST
Structure
controllers
models
views
routes.rb, database.yml
Gemfile
MVC
controller
model view
browser
DB
routes
web server
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>
<html>
…
<body>
…
<%= yield %>
…
</body>
</html
http://localhost:3000/http://localhost:3000/
Let’s code
Thanks

Everyday Rails