Everyday Rails brought by Blaze Hadzik and
Blaze HadzikRuby on Rails developerblazej.hadzik@netguru.co
Software houseweb&mobile
Poznan, Warsaw, Gdansk, Krakow Australia, India, America, Brazil
teams with mentors
‘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 = 12a.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 = 0HIGH_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 statementscase statementswhile structure
Idioms
a, b = 1, 2a += ba # => 3
@a ||= 1 # @a = 1 if @a.nil?
Classes, objects
class Schooldef initialize(name)
@name = nameend
end
school = School.new(‘polsl’)p school # =>#<School:0x007fa301836160 @name="polsl">
Class Attributesclass School
def initialize(name)@name = name
end
def name=(name)@name = name
end
def name@name
endend
Class Attributes
attr_readerattr_writer
attr_accessor
Access Control
privateprotected
public
‘It is impossible not to notice Ruby on Rails.’Martin Fowler
Ruby is languageRails is framework
Convention over Configuration
controllers
modelsviews
routes.rb, database.yml
Gemfile
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 endend
class Post < ActiveRecord::Baseend
class Post < ActiveRecord::Baseend
class WelcomeController < ApplicationController def index @posts = Post.all endend
<ul> <% @posts.each do |post| %> <li> <%= post.title %> </li> <% end %></ul>
<html> … <body> … <%= yield %> … </body></html
http://localhost:3000/http://localhost:3000/