Ruby on Rails

Michał Zdunek

Dlaczego Rails?

  • Bardzo popularny framework - używany m.in. przez GitHub, Twitch, Airbnb
  • Premiera w 2004 - wiele lat w rozwoju
  • Opiera się na zasadach: Convention over Configuration, DRY etc.
  • Napisany w wygodnym języku Ruby

Ruby - składnia

$ irb
>> def get_error type = :none
>>     if type == :none
>>         "No error"
>>     elsif type == :memory
>>         "Memory error"
>>     else
>>         "Other error"
>>     end
>> end
>> get_error
=> "No error"
>> get_error :memory
=> "Memory error"

Ruby - hashe

>> capitals = {:poland => "Warsaw", :russia => "St Petersburg"}
=> {:poland=>"Warsaw", :russia=>"St Petersburg"}
>> capitals["poland"] = "Warszawa"
=> "Warszawa"
>> capitals[:russia] = "Moscow"
=> "Moscow"
>> capitals
=> {:poland=>"Warsaw", :russia=>"Moscow", "poland"=>"Warszawa"}
>> capitals[:russia]
=> "Moscow"

Ruby - bloki

>> [1, 2].each {|n| puts n}
1
2
>> [1, 2].each do |n|
>>     puts n
>> end
1
2
>> [1, 2].map {|n| n + 2}
=> [3, 4]

Ruby - klasy

class Example
    def initialize id
        @id = id
    end

    def self.get_id
        0
    end

    def get_id
        @id
    end
end

>> example = Example.new 2
=> #<Example:0x0000000250f580 @id=2>
>> example.get_id
=> 2
>> Example.get_id
=> 0

Ruby - community

  • Rails - projekt open source
  • Lokalne grupy użytkowników - m.in. Poznań Ruby User Group
  • Wielka biblioteka gemów (dodatków): rubygems.org
$ gem install rails

Rails - architektura MVC

Szablony w Rails

#content
  .left.column
    %p= print_information
  .right.column
    - if sidebar
      Site sidebar
  • Haml
<div id="content">
  <div class="left column">
    <p><%= print_information %></p>
  </div>
  <div class="right column">
    <% if sidebar %>
      Site sidebar
    <% end %>
  </div>
</div>
  • ERB

DEMO

deck

By mzdunek93

deck

  • 605