Всё возвращает значение def sum ( a , b ) a + b end a = if true 'правда' else 'ложь' end #=> 'правда'
Функция как аргумент [ 1 , 2 , 3 ]. find { | i | i % 2 == 0 } #=> 2
Работа с массивами 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
Объектный -1 . abs () #=> 1 SomeClass. new #=> объект класса 2 .+( 3 ) #=> 5
Динамический class String def /( another ) File. join ( self , another ) end end 'dir' / 'file' #=> "dir/file"
Пример. Тестирование. RSpec describe Users do it 'should add user' do # Метод empty? должен вернуть true Users. should be_empty Users << 'ai' Users. length . should == 1 end end
Пример. 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 #=> "Первая запись"
Пример. Веб. 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
3 человека + 48 часов + Ruby =
Ссылки Руби: Обучение: Встречи: Книги: Изучаем Ruby Ruby on Rails. Быстрая веб-разработка. Путь Rails ruby-lang.org bit.ly/BPCg3 twitter.com/spbruby
0 comments
Post a comment