36
Ruby outside Rails Victor Petrenko Software Engineer & CTO at Songsterr [email protected]

Ruby Outside Rails 2 (southfest)

Embed Size (px)

DESCRIPTION

Ruby outside rails (version for Southfest)

Citation preview

Page 1: Ruby Outside Rails 2 (southfest)

Ruby outside Rails

Victor Petrenko Software Engineer & CTO at Songsterr

[email protected]

Page 2: Ruby Outside Rails 2 (southfest)

План

• Автоматизируем свою работу

• Capistrano. Деплоймент не только Ruby on Rails

• Domain Specific Languages. Зачем и как?

Page 3: Ruby Outside Rails 2 (southfest)

Раздел 1'!

Автоматизируем свою работу

Page 4: Ruby Outside Rails 2 (southfest)

Задача первая

• Обработать информацию из e-mail-ов

• Есть специальная IMAP папка, куда приходят отчеты

Page 5: Ruby Outside Rails 2 (southfest)
Page 6: Ruby Outside Rails 2 (southfest)

Задача вторая

• Набор данных (координат) • Обработать, преобразовать

Page 7: Ruby Outside Rails 2 (southfest)
Page 8: Ruby Outside Rails 2 (southfest)

Исходные координаты:39.09185372259229,47.03224879566123 39.101477488537704,47.03835893695837 39.10424821045009,47.04039977268962 39.10554774071781,47.04133214996279 39.102158937265564,47.042633164691466 39.10220336135238,47.04308340712977 39.106841236015235,47.04163697966206 39.10499889369813, 47.03482143985691 39.10768261145561,47.0309732011439 39.11173962022869,47.027242062794656 39.11528281833736,47.014814887230955 39.09256182577221,47.01383342906308 39.08008418943492,47.01500016980724 39.07790623571488,47.0147580181646 39.0634223070345,47.01051651649383 39.05895911123376,47.01867633605033 39.07149039175134,47.025778466934604 39.076082333584786,47.02739245501852 39.086682423611634,47.02959326869004 39.09185372259229,47.03224879566123

Page 9: Ruby Outside Rails 2 (southfest)

Что хотим получить:[[47.03224879566123, 39.09185372259229], [47.03835893695837, 39.101477488537704], [47.04039977268962, 39.10424821045009], [47.04133214996279, 39.10554774071781], [47.042633164691466, 39.102158937265564], [47.04308340712977, 39.10220336135238], [47.04163697966206, 39.106841236015235], [39.10499889369813], [47.03482143985691], [47.0309732011439, 39.10768261145561], [47.027242062794656, 39.11173962022869], [47.014814887230955, 39.11528281833736], [47.01383342906308, 39.09256182577221], [47.01500016980724, 39.08008418943492], [47.0147580181646, 39.07790623571488], [47.01051651649383, 39.0634223070345], [47.01867633605033, 39.05895911123376], [47.025778466934604, 39.07149039175134], [47.02739245501852, 39.076082333584786], [47.02959326869004, 39.086682423611634], [47.03224879566123, 39.09185372259229]]

39.09185372259229,47.03224879566123 39.101477488537704,47.03835893695837 39.10424821045009,47.04039977268962 39.10554774071781,47.04133214996279 39.102158937265564,47.042633164691466 39.10220336135238,47.04308340712977 39.106841236015235,47.04163697966206 39.10499889369813, 47.03482143985691 39.10768261145561,47.0309732011439 39.11173962022869,47.027242062794656 39.11528281833736,47.014814887230955 39.09256182577221,47.01383342906308 39.08008418943492,47.01500016980724 39.07790623571488,47.0147580181646 39.0634223070345,47.01051651649383 39.05895911123376,47.01867633605033 39.07149039175134,47.025778466934604 39.076082333584786,47.02739245501852 39.086682423611634,47.02959326869004 39.09185372259229,47.03224879566123

Page 10: Ruby Outside Rails 2 (southfest)

# -e Execute one-liner

ruby -e "while line = gets; print line.chomp; end” data.txt

Page 11: Ruby Outside Rails 2 (southfest)

# -ne - wrap one-liner in while

ruby -ne "print \$_.chomp" data.txt

Page 12: Ruby Outside Rails 2 (southfest)

Demo

Page 13: Ruby Outside Rails 2 (southfest)

topbook-2:demo$ cat kazachok'39.09185372259229,47.03224879566123 39.101477488537704,47.03835893695837 39.10424821045009,47.04039977268962 39.10554774071781,47.04133214996279 39.102158937265564,47.042633164691466 39.10220336135238,47.04308340712977 39.106841236015235,47.04163697966206 39.10499889369813, 47.03482143985691 39.10768261145561,47.0309732011439 39.11173962022869,47.027242062794656 39.11528281833736,47.014814887230955 39.09256182577221,47.01383342906308 39.08008418943492,47.01500016980724 39.07790623571488,47.0147580181646 39.0634223070345,47.01051651649383 39.05895911123376,47.01867633605033 39.07149039175134,47.025778466934604 39.076082333584786,47.02739245501852 39.086682423611634,47.02959326869004 39.09185372259229,47.03224879566123

Page 14: Ruby Outside Rails 2 (southfest)

topbook-2:demo$ ruby -ne "print chomp.split” kazachok'["39.09185372259229,47.03224879566123", "39.101477488537704,47.03835893695837", "39.10424821045009,47.04039977268962", "39.10554774071781,47.04133214996279", "39.102158937265564,47.042633164691466", "39.10220336135238,47.04308340712977", "39.106841236015235,47.04163697966206", "39.10499889369813,", "47.03482143985691", "39.10768261145561,47.0309732011439", "39.11173962022869,47.027242062794656", "39.11528281833736,47.014814887230955", "39.09256182577221,47.01383342906308", "39.08008418943492,47.01500016980724", "39.07790623571488,47.0147580181646", "39.0634223070345,47.01051651649383", "39.05895911123376,47.01867633605033", "39.07149039175134,47.025778466934604", "39.076082333584786,47.02739245501852", "39.086682423611634,47.02959326869004", "39.09185372259229,47.03224879566123"]

Page 15: Ruby Outside Rails 2 (southfest)

demo$ ruby -ne "print chomp.split.map { |x| x.split(',') }" kazachok'

[["39.09185372259229", "47.03224879566123"], ["39.101477488537704", "47.03835893695837"], ["39.10424821045009", "47.04039977268962"], ["39.10554774071781", "47.04133214996279"], ["39.102158937265564", "47.042633164691466"], ["39.10220336135238", "47.04308340712977"], ["39.106841236015235", "47.04163697966206"], ["39.10499889369813"], ["47.03482143985691"], ["39.10768261145561", "47.0309732011439"], ["39.11173962022869", "47.027242062794656"], ["39.11528281833736", "47.014814887230955"], ["39.09256182577221", "47.01383342906308"], ["39.08008418943492", "47.01500016980724"], ["39.07790623571488", "47.0147580181646"], ["39.0634223070345", "47.01051651649383"], ["39.05895911123376", "47.01867633605033"], ["39.07149039175134", "47.025778466934604"], ["39.076082333584786", "47.02739245501852"], ["39.086682423611634", "47.02959326869004"], ["39.09185372259229", "47.03224879566123"]]

Page 16: Ruby Outside Rails 2 (southfest)

"print chomp.split.map {|x| x.split(',').reverse}" kazachok'[["47.03224879566123", "39.09185372259229"], ["47.03835893695837", "39.101477488537704"], ["47.04039977268962", "39.10424821045009"], ["47.04133214996279", "39.10554774071781"], ["47.042633164691466", "39.102158937265564"], ["47.04308340712977", "39.10220336135238"], ["47.04163697966206", "39.106841236015235"], ["39.10499889369813"], ["47.03482143985691"], ["47.0309732011439", "39.10768261145561"], ["47.027242062794656", "39.11173962022869"], ["47.014814887230955", "39.11528281833736"], ["47.01383342906308", "39.09256182577221"], ["47.01500016980724", "39.08008418943492"], ["47.0147580181646", "39.07790623571488"], ["47.01051651649383", "39.0634223070345"], ["47.01867633605033", "39.05895911123376"], ["47.025778466934604", "39.07149039175134"], ["47.02739245501852", "39.076082333584786"], ["47.02959326869004", "39.086682423611634"], ["47.03224879566123", "39.09185372259229"]]

Page 17: Ruby Outside Rails 2 (southfest)

ruby -ne "print chomp.split.map { |x| x.split(',').reverse.map(&:to_f)}" kazachok'[[47.03224879566123, 39.09185372259229], [47.03835893695837, 39.101477488537704], [47.04039977268962, 39.10424821045009], [47.04133214996279, 39.10554774071781], [47.042633164691466, 39.102158937265564], [47.04308340712977, 39.10220336135238], [47.04163697966206, 39.106841236015235], [39.10499889369813], [47.03482143985691], [47.0309732011439, 39.10768261145561], [47.027242062794656, 39.11173962022869], [47.014814887230955, 39.11528281833736], [47.01383342906308, 39.09256182577221], [47.01500016980724, 39.08008418943492], [47.0147580181646, 39.07790623571488], [47.01051651649383, 39.0634223070345], [47.01867633605033, 39.05895911123376], [47.025778466934604, 39.07149039175134], [47.02739245501852, 39.076082333584786], [47.02959326869004, 39.086682423611634], [47.03224879566123, 39.09185372259229]]

Page 18: Ruby Outside Rails 2 (southfest)

Ruby One-Liners

• http://benoithamelin.tumblr.com/post/10945200630/text-processing-1liners-ruby-vs-awk

• http://benoithamelin.tumblr.com/ruby1line/

Page 19: Ruby Outside Rails 2 (southfest)

Раздел 2 !

Capistrano

Page 20: Ruby Outside Rails 2 (southfest)

Capistrano• A remote server automation and deployment tool

written in Ruby

Page 21: Ruby Outside Rails 2 (southfest)

Multi-staging Deployment

• Окружение разработчика

• Source Control Management (GIT)

• Test Server (Build, Unit Tests, Staging, UI Tests)

• Production Server (Production)

Page 22: Ruby Outside Rails 2 (southfest)

Multi-staging Deployment

set :stages, %w(production staging)!

set :default_stage, "staging"!

require 'capistrano/ext/multistage'

Page 23: Ruby Outside Rails 2 (southfest)

Multi-staging Deployment• Staging

• Production

Page 24: Ruby Outside Rails 2 (southfest)

Как это работает?$ pwd /u/apps/AppName !$ ls -ls lrwxrwxrwx current -> /u/apps/AppName/releases/20140827175714 drwxrwxr-x releases drwxrwxr-x shared !$ ls -ls releases/ drwxrwxr-x 20140731111527 drwxrwxr-x 20140731120229 drwxrwxr-x 20140802135509 drwxrwxr-x 20140825110237 drwxrwxr-x 20140827175714

Page 25: Ruby Outside Rails 2 (southfest)

Раздел 3 !

Domain Specific

Languages'(DSL)

Page 26: Ruby Outside Rails 2 (southfest)

Domain Specific Languages

• DSL — специализированный язык для определенной предметной области!

• Примеры DSL: MATLAB, SQL, GraphViz!

• DSL на Ruby: Capistrano, ActiveRecord, Rails!

• Создание DSL: listo

Page 27: Ruby Outside Rails 2 (southfest)

listo - Система сборки• Большой проект на С++ (30+ подпроектов, 50+ разработчиков)

• Единообразие настроек компилятора

• Кросс-платформенная компиляция (Windows, Unix)

• Интроспекция проекта (проверки, тесты)

Page 28: Ruby Outside Rails 2 (southfest)
Page 29: Ruby Outside Rails 2 (southfest)
Page 30: Ruby Outside Rails 2 (southfest)

class Maker

Page 31: Ruby Outside Rails 2 (southfest)

class World

Page 32: Ruby Outside Rails 2 (southfest)

Success Story• Решал задачи, прожил ~2 года

• Тонкое управление организацией проекта

• Генерация MS Visual Studio projects/solutions

• Генерация qmake файлов (для сборки в unix)

• Автоматическое включение исходников в проект

Page 33: Ruby Outside Rails 2 (southfest)

Проблемы DSL на Ruby• Безопасность

• Невнятные сообщения об ошибках

• Кросс-платформенность

• Скорость

• Синтаксические ограничения

Page 34: Ruby Outside Rails 2 (southfest)

Migration

Page 35: Ruby Outside Rails 2 (southfest)

Ссылки

• Paolo Perrotta - Metaprogramming Ruby 2

• http://www.dan-manges.com/blog/ruby-dsls-instance-eval-with-delegation

• http://en.wikipedia.org/wiki/Domain-specific_language

Page 36: Ruby Outside Rails 2 (southfest)

Спасибо за внимание!

Вопросы?

Виктор Петренко [email protected]