gigigo ruby workshop

15
Ruby Workshop @alexruperez

Upload: alex-ruperez

Post on 29-Jun-2015

257 views

Category:

Engineering


2 download

DESCRIPTION

Ruby Workshop at Gigigo Mobile Services. See also: https://github.com/gigigoapps/Ruby-Rails-Workshop https://github.com/alexruperez/sinatra-unicorn-heroku-sample http://www.sinatrarb.com

TRANSCRIPT

  • 1. Ruby Workshop@alexruperez

2. Operations2 + 63 * 44 - 910 / 55 % 2 3. Strings"String with whites"'String':symbol"String with whites".reverse"String with whites".length'Composite' + ' ' + 'String''String' * 5 4. Basicsmy_array = ['Element1', 'Element2', 'Element3']my_dict = {name: 'Alex', surname: 'Ruprez'}puts "my_array contains #{my_array.count} elements"puts "my_array is empty" if my_array.empty?puts "My name is #{my_dict[:name]}" unless!my_dict[:name]first_element = my_array.empty? ? 'Unknown' :my_array.firstmy_dict[:surname] ||= "Smith" 5. Variableslocal$global@instance@@class 6. Methodsdef sum(param1, param2)param1 + param2enddef my_method paramif paramreturn paramelsereturn nilendend 7. Classes class MyClassdef initialize(prop1,prop2)@prop1 = prop1@prop2 = prop2enddef sum@prop1 + @prop2endendobj = MyClass.new(5, 15)puts obj.sum 8. Error Handlingbeginputs 1/0rescue => my_errorputs my_errorendvalue = 1/0 rescue nil 9. Sinatra + Unicorn + Heroku 10. Gemfilesource'https://rubygems.org'gem 'unicorn'gem 'sinatra' 11. web.rbrequire 'sinatra'set :bind,'0.0.0.0'get '/' do"Hello world!"end 12. config.rurequire './web'runSinatra::Application 13. unicorn.rbworker_processes 3timeout 15preload_app true 14. Procfileweb: bundle exec unicorn -p $PORT -c./unicorn.rb 15. Thanks!@alexruperez