19. Работа с массивами 5 . times { | i | puts i } [ 1 , 2 , 3 ]. each { | i | puts i } [ 1 , 2 , 3 ]. map { | i | i + 1 } #=> [2, 3, 4] [ 1 , 2 , 3 ]. inject { | sum , i | sum + i } #=> 6
20. Объектный -1 . abs () #=> 1 SomeClass. new #=> объект класса 2 .+( 3 ) #=> 5
21. Динамический class String def /( another ) File. join ( self , another ) end end 'dir' / 'file' #=> "dir/file"
22. Пример. Тестирование. RSpec describe Users do it 'should add user' do # Метод empty? должен вернуть true Users. should be_empty Users << 'ai' Users. length . should == 1 end end
23. Пример. ORM. ActiveRecord class Author < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belong_to :author end ai = Author. find_by_login ( 'ai' ) ai . login #=> "ai" ai . posts . length #=> 3 post = ai. posts . first post . title #=> "Первая запись"
24. Пример. Веб. Sinatra site.rb get '/post/:id' do post = Post. find ( params [ 'id' ]) haml :post end views/post.haml %html %body %h1 = post .title %p = post .text