Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Защо Ruby и Rails ?Радослав СтанковRuby on Rails семинар – Варненски свободен университет 2010
Кой съм аз?@rstankovhttp://rstankov.comhttp://blog.rstankov.comhttp://github.com/rstankov
RubyRuby е интерактивен,обектно-ориентиран език запрограмиране.Неготово мото е:“Optimized for programmer happiness”
Ruby on Rails   Ruby on Rails е framework   за уеб приложения.   Неготово мото е:   “Web development doesn’t hurt”
Ruby Създаден от японеца Юкихиро Мацумото „Мац“. Първата публична версия излиза през 1995. Версия 2.0 се очакава да излезе...
private $_products = null;public function getProducts($categoryId){  if ($this->_products === null){    $this->_products =...
def products(category_id)  @products ||= Product.where({ :category_id => category_id })end
def products(category_id)  @products ||= Product.where({ :category_id => category_id })end
def products(category_id)  @products ||= Product.where({ :category_id => category_id })end
def products(category_id)  @products ||= Product.where({ :category_id => category_id })end
def products(category_id)  @products ||= Product.where({ :category_id => category_id })end
def products(category_id)  @products ||= Product.where({ :category_id => category_id })end
def products(category_id)  @products ||= Product.where( :category_id => category_id )end
def products(category_id)  @products ||= Product.where :category_id => category_idend
5.times { puts "Ruby is cool!" }5.times do  puts "Ruby is cool!"end
File.open("some_file.txt") do |file|  file << "content"end
[1,2,3,4].each { |value| puts value }# => [1, 2, 3, 4][1,2,3,4].map { |value| value * value }# => [1, 4, 9, 16][1,2,3,4].r...
def operate(value)  yield valueendvalue = operate(5) { |v| v + 4 }puts value # 9
class Person  attr_accessor :fist_name, :last_name  def initialize(first_name, last_name)    @first_name = first_name    @...
class Person  attr_accessor :fist_name, :last_name  def initialize(first_name, last_name)    @first_name = first_name    @...
class Person  attr_accessor :fist_name, :last_name  def initialize(first_name, last_name)    @first_name = first_name    @...
class Person  attr_accessor :fist_name, :last_name  def initialize(first_name, last_name)    @first_name = first_name    @...
class Person  attr_accessor :fist_name, :last_name  def initialize(first_name, last_name)    @first_name = first_name    @...
class Person  def +(other_person)    # ...  endendperson1 + person2
class Array  def first    self[0]  end  def second    self[1]  endendarray = [1, 2, 3, 4]array.first   # 1array.second # 2
class Numeric  def plus(x)    self + x  endendy = 5.plus(6)
Time.now - 2.days == 2.days.ago1.month.ago + 45.days - 1.year
Ruby•   димично определяне на типа данните•   модули•   отворени класове•   блокове•   анонимни функции•   всяка операция ...
Ruby on Rails          Разработен от David     Heinemeier Hansson от 37     Signals.     Първата публична версия е     изл...
37signals  Милиони клиенти ... по-малко от 37 служителя
MVC Архитектура
Начин на работа
Modelclass Task < ActiveRecord::Base  belongs_to :user  has_many :comments  validates_presence_of :user, :text  validates_...
Modelclass Task < ActiveRecord::Base  belongs_to :user  has_many :comments  validates_presence_of :user, :text  validates_...
Modelclass Task < ActiveRecord::Base  belongs_to :user  has_many :comments  validates_presence_of :user, :text  validates_...
Modelclass Task < ActiveRecord::Base  belongs_to :user  has_many :comments  validates_presence_of :user, :text  validates_...
Modelclass Task < ActiveRecord::Base  belongs_to :user  has_many :comments  validates_presence_of :user, :text  validates_...
Controllerclass TasksController < ApplicationController  def index    @tasks = Tasks.where(:status => "opened")  end  def ...
Controllerclass TasksController < ApplicationController  def index    @tasks = Tasks.where(:status => "opened")  end  def ...
Controllerclass TasksController < ApplicationController  def index    @tasks = Tasks.where(:status => "opened")  end  def ...
Controllerclass TasksController < ApplicationController  def index    @tasks = Tasks.where(:status => "opened")  end  def ...
View<%= form_for task do |form| %>  <%= render shared/error_messages, :target => form.object %>  <p>    <%= form.label :te...
Test Driven Development
Test Driven Development1   Добавя се тест    ... за несъществуващ код
Test Driven Development1   Добавя се тест    ... за несъществуващ код                               2   Пише се код       ...
Test Driven Development    1     Добавя се тест          ... за несъществуващ код                                         ...
Test Driven Development    1     Добавя се тест          ... за несъществуващ код                                         ...
describe Task do  it { should belong_to(:user) }  it { should have_many(:notes) } it { should validate_presence_of(:user) ...
Тесване на Controllerdescribe TasksController do  describe "GET show" do    before { Task.should_recive(:find).with("1").a...
Тестване на Viewdescribe "tasks/show.html.erb" do  before do    assign :task, mock_model(Task, {       :id   => 1,       :...
Благодаря за вниманието@rstankov
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Why ruby
Upcoming SlideShare
Loading in …5
×

Why ruby

1,424 views

Published on

My presentation from Ruby on Rails seminar in Varna at Varna Free University "Chernorizets Hrabar"

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Why ruby

  1. 1. Защо Ruby и Rails ?Радослав СтанковRuby on Rails семинар – Варненски свободен университет 2010
  2. 2. Кой съм аз?@rstankovhttp://rstankov.comhttp://blog.rstankov.comhttp://github.com/rstankov
  3. 3. RubyRuby е интерактивен,обектно-ориентиран език запрограмиране.Неготово мото е:“Optimized for programmer happiness”
  4. 4. Ruby on Rails Ruby on Rails е framework за уеб приложения. Неготово мото е: “Web development doesn’t hurt”
  5. 5. Ruby Създаден от японеца Юкихиро Мацумото „Мац“. Първата публична версия излиза през 1995. Версия 2.0 се очакава да излезе за Коледа / не се казва коя /
  6. 6. private $_products = null;public function getProducts($categoryId){ if ($this->_products === null){ $this->_products = Product::where(array( category_id => $categoryId )); } return $this->products;}
  7. 7. def products(category_id) @products ||= Product.where({ :category_id => category_id })end
  8. 8. def products(category_id) @products ||= Product.where({ :category_id => category_id })end
  9. 9. def products(category_id) @products ||= Product.where({ :category_id => category_id })end
  10. 10. def products(category_id) @products ||= Product.where({ :category_id => category_id })end
  11. 11. def products(category_id) @products ||= Product.where({ :category_id => category_id })end
  12. 12. def products(category_id) @products ||= Product.where({ :category_id => category_id })end
  13. 13. def products(category_id) @products ||= Product.where( :category_id => category_id )end
  14. 14. def products(category_id) @products ||= Product.where :category_id => category_idend
  15. 15. 5.times { puts "Ruby is cool!" }5.times do puts "Ruby is cool!"end
  16. 16. File.open("some_file.txt") do |file| file << "content"end
  17. 17. [1,2,3,4].each { |value| puts value }# => [1, 2, 3, 4][1,2,3,4].map { |value| value * value }# => [1, 4, 9, 16][1,2,3,4].reduce { |a, b| a + b }# => 10
  18. 18. def operate(value) yield valueendvalue = operate(5) { |v| v + 4 }puts value # 9
  19. 19. class Person attr_accessor :fist_name, :last_name def initialize(first_name, last_name) @first_name = first_name @last_name = last_name end def full_name "#{first_name} + #{last_name}" endendme = Person.new("Radoslav", "Stankov")puts me.full_name # Radoslav Stankov
  20. 20. class Person attr_accessor :fist_name, :last_name def initialize(first_name, last_name) @first_name = first_name @last_name = last_name end def full_name first_name + " " + last_name endendme = Person.new("Radoslav", "Stankov")puts me.full_name # Radoslav Stankov
  21. 21. class Person attr_accessor :fist_name, :last_name def initialize(first_name, last_name) @first_name = first_name @last_name = last_name end def full_name first_name + " " + last_name endendme = Person.new("Radoslav", "Stankov")puts me.full_name # Radoslav Stankov
  22. 22. class Person attr_accessor :fist_name, :last_name def initialize(first_name, last_name) @first_name = first_name @last_name = last_name end def full_name first_name + " " + last_name endendme = Person.new("Radoslav", "Stankov")puts me.full_name # Radoslav Stankov
  23. 23. class Person attr_accessor :fist_name, :last_name def initialize(first_name, last_name) @first_name = first_name @last_name = last_name end def full_name first_name + " " + last_name endendme = Person.new("Radoslav", "Stankov")puts me.full_name # Radoslav Stankov
  24. 24. class Person def +(other_person) # ... endendperson1 + person2
  25. 25. class Array def first self[0] end def second self[1] endendarray = [1, 2, 3, 4]array.first # 1array.second # 2
  26. 26. class Numeric def plus(x) self + x endendy = 5.plus(6)
  27. 27. Time.now - 2.days == 2.days.ago1.month.ago + 45.days - 1.year
  28. 28. Ruby• димично определяне на типа данните• модули• отворени класове• блокове• анонимни функции• всяка операция връща резултат• всичко е изпълним код• изчистен синтаксис• мета програмиране• промяна на обектите по време на изпълнение• вградени регулярни изрази
  29. 29. Ruby on Rails Разработен от David Heinemeier Hansson от 37 Signals. Първата публична версия е излиза през юли 2004. Текущата стабилна версия е 3.0.3
  30. 30. 37signals Милиони клиенти ... по-малко от 37 служителя
  31. 31. MVC Архитектура
  32. 32. Начин на работа
  33. 33. Modelclass Task < ActiveRecord::Base belongs_to :user has_many :comments validates_presence_of :user, :text validates_inclusion_of :status, :in => ["opened", "completed"] attr_readonly :user_id def editable? status == "opened" endend
  34. 34. Modelclass Task < ActiveRecord::Base belongs_to :user has_many :comments validates_presence_of :user, :text validates_inclusion_of :status, :in => ["opened", "completed"] attr_readonly :user_id def editable? status == "opened" endend
  35. 35. Modelclass Task < ActiveRecord::Base belongs_to :user has_many :comments validates_presence_of :user, :text validates_inclusion_of :status, :in => ["opened", "completed"] attr_readonly :user_id def editable? status == "opened" endend
  36. 36. Modelclass Task < ActiveRecord::Base belongs_to :user has_many :comments validates_presence_of :user, :text validates_inclusion_of :status, :in => ["opened", "completed"] attr_readonly :user_id def editable? status == "opened" endend
  37. 37. Modelclass Task < ActiveRecord::Base belongs_to :user has_many :comments validates_presence_of :user, :text validates_inclusion_of :status, :in => ["opened", "completed"] attr_readonly :user_id def editable? status == "opened" endend
  38. 38. Controllerclass TasksController < ApplicationController def index @tasks = Tasks.where(:status => "opened") end def show @task = Task.find(params[:id]) end def create @task = current_user.tasks.build(params[:task]) if @task.save redirect_to tasks_path else render :partial => "new" end endend
  39. 39. Controllerclass TasksController < ApplicationController def index @tasks = Tasks.where(:status => "opened") end def show @task = Task.find(params[:id]) end def create @task = current_user.tasks.build(params[:task]) if @task.save redirect_to tasks_path else render :partial => "new" end endend
  40. 40. Controllerclass TasksController < ApplicationController def index @tasks = Tasks.where(:status => "opened") end def show @task = Task.find(params[:id]) end def create @task = current_user.tasks.build(params[:task]) if @task.save redirect_to tasks_path else render :partial => "new" end endend
  41. 41. Controllerclass TasksController < ApplicationController def index @tasks = Tasks.where(:status => "opened") end def show @task = Task.find(params[:id]) end def create @task = current_user.tasks.build(params[:task]) if @task.save redirect_to tasks_path else render :partial => "new" end endend
  42. 42. View<%= form_for task do |form| %> <%= render shared/error_messages, :target => form.object %> <p> <%= form.label :text %> <%= form.text_area :text %> </p> <p> <%= form.label :status %> <%= form.select :status, Task.statuses %> </p> <div class="buttons"> <%= form.submit %> | <% link_to "Cancel", task_path %> </div><% end %>
  43. 43. Test Driven Development
  44. 44. Test Driven Development1 Добавя се тест ... за несъществуващ код
  45. 45. Test Driven Development1 Добавя се тест ... за несъществуващ код 2 Пише се код ... колкото само тестът да мине
  46. 46. Test Driven Development 1 Добавя се тест ... за несъществуващ код 2 Пише се код ... колкото само тестът да мине3 Правят се подобрения ... подобрява се качеството на кода
  47. 47. Test Driven Development 1 Добавя се тест ... за несъществуващ код 2 Пише се код ... колкото само тестът да мине3 Правят се подобрения ... подобрява се качеството на кода
  48. 48. describe Task do it { should belong_to(:user) } it { should have_many(:notes) } it { should validate_presence_of(:user) } it { should validate_presence_of(:text) } describe "#editable?" do it "returns true if task status is opened" do Task.new(:status => "opened").should be_editable end it "returns false if task status is rejected" do Task.new(:status => "rejected").should_not be_editable end it "returns false if task status is completed" do Task.new(:status => "completed").should_not be_editable end endend
  49. 49. Тесване на Controllerdescribe TasksController do describe "GET show" do before { Task.should_recive(:find).with("1").and_return task } before { get :show, :id => "1" } it { should assign_to(:task).with(task) } it { should render_temlate("show") } endend
  50. 50. Тестване на Viewdescribe "tasks/show.html.erb" do before do assign :task, mock_model(Task, { :id => 1, :text => "task text" }) render end it "renders task text" do rendered.should contain("task text") end it "renders link for editing task" do rendered.should have_selector(a[href="/tasks/1/edit"]) endend
  51. 51. Благодаря за вниманието@rstankov

×