Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hello World 2018 - Why Ruby?

Hello World 2018 - Why Ruby?

Hello World Tech Conference

February 15, 2018
Tweet

More Decks by Hello World Tech Conference

Other Decks in Programming

Transcript

  1. Why Ruby? - Porto, 15/2/2018 2 Warning You are about

    to see inconsistent and poorly designed slides
  2. Why Ruby? Old Stuff - Java - PHP - Python

    - Ruby New stuff - Elixir - Erlang - Javascript - GO - Node.js - Functional Programming Too many choices!
  3. Why Ruby? - Porto, 15/2/2018 It is beginner-friendly - Scripting

    language - Everything is an object - Weakly typed (“duck type”) - No semicoloms ; - Parenthesis can be omitted - Implicit returns - Syntax reads like English - Friendly Community - Lots of Resources - Awesome standard library - Designed to be fun 5
  4. Why Ruby? - Porto, 15/2/2018 8 # Gemfile source 'https://rubygems.org'

    gem 'capistrano' gem 'capistrano-bundler' gem 'capistrano-chruby' gem 'capistrano-rails' gem 'clearance', '~>1.14.1' gem 'dotenv-rails' gem 'foundation-icons-sass-rails' gem 'foundation-rails' gem 'koine-csv', '~> 0.2.1' gem 'koine-google_maps_client' gem 'koine-db_bkp', '~> 0.1.2' gem 'object_comparator' gem 'pg' gem 'sentry-raven' gem 'simple_form', '~> 3.5' gem 'sprockets' gem 'sprockets-es6' gem 'unicorn' gem 'wicked_pdf' gem 'wkhtmltopdf-binpath' gem 'whenever', require: false gem 'rack-cors', require: ‘rack/cors' # bundle install # done!
  5. Why Ruby? - Porto, 15/2/2018 9 # Rakefile namespace :test

    do desc "run Javascript tests" task :javascript do exec 'yarn run test' end end # Commands rake -T | grep javascript rake test:javascript # run Javascript tests rake test:javascript
  6. Why Ruby? - Porto, 15/2/2018 10 # deploy to your

    configured servers $ cap production deploy
  7. Why Ruby? - Porto, 15/2/2018 RAPID development framework - Convention

    over configuration - Hides stuff you do not need to know - Eliminates tedious tasks - Console - Command line utilities - Database abstraction - Scaffolding for CRUD - Active Record - Security concerns - (SQL Injection, XSS, CSRS tokens) - Active Record - Migrations - devs can change db independently without stepping into each other’s toes - Full stack - MVC - It helps you to become a better programmer - ~14 years old - Everyone wants to be rails 11
  8. Rails Utilities 12 rails new my_app_name d mysql rake db:create

    rake db:migrate rails generate migration create_users_table username:string email:string:uniq rails db:migrate rails generate controller home rails server # http://localhost:3000
  9. 15

  10. Why Ruby? - Porto, 15/2/2018 • 378K shops • 80K

    peak RPM • Average: 20-40K RPS • Ruby on Rails since 2006 • 2 Data centres • 40+ deploys 16
  11. Why Ruby? - Porto, 15/2/2018 • 45 Teams • 1200+

    employees • 90+ ruby apps (85 rails) • Rails since 2008 • Shared functionalities through ruby gems • 40M members! 17
  12. Printing to the standard output 19 // PHP echo “Hello

    World" # RUBY puts “Hello World"
  13. Arrays 20 // PHP $numbers = [1, 2, 3, 4,

    5, 6, 7, 8, 9, 10]; # RUBY numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # or numbers = (1..10).to_a # or numbers = Range.new(1, 10).to_a
  14. Looping through an array 22 # RUBY numbers.each do |num|

    # puts num end # Alternative one-liner: numbers.each { |num| puts num } # with index numbers.each_with_index do |num, index| # puts "#{num} @#{index}" end // PHP foreach ($numbers as $num) { // echo $num; } // with index foreach ($numbers as $index => $num) { // echo "$num @$index" }
  15. Array interactions 23 // sum $sum = array_sum($numbers); // doubles

    $doubles = array_map(function ($num) { return $number * 2; }, $numbers); // even $even = array_filter($numbers, function ($num) { return $num % 2 == 0; }); // even $firstGreaterThanFive = null; foreach ($numbers as $num) { if ($num > 5) { $firstGreaterThanFive = $num; break; } } # sum sum = numbers.sum # 55 # doubles numbers.map { |num| num * 2 } # even even = numbers.select { |num| num % 2 == 0 } even = numbers.select { |num| num.even? } even = numbers.select(&:even?) even = numbers.reject(&:odd?) # odds first_greater_than_five = numbers.find do |num| num > 5 end
  16. Array interactions 24 # all? [2, 4, 6].all? { |e|

    e.even? } # returns true [2, 3, 6].all? { |e| e.even? } # returns false [2, 3, 6].any? { |e| e.even? } # returns true [3, 4].any? # returns true [].any? # returns false [nil].any? # returns false [false].any? # returns false
  17. Array interactions - Sorting arrays 25 # sort [7, 2,

    5].sort # returns [2, 5, 7] ['c', 'b', 'a'].sort # returns ['a', 'b', 'c'] employees.sort_by {|e| e.last_name} # sort your employees by last name employees.sort_by(&:last_name) # Alternatively
  18. Hashes 26 hash = { one: 'The One', two: 'Not

    the one', 'three' => 'None of the above', } hash[:one] # 'the one' hash['three'] # ‘None of the above’ hash['non-existing'] # nil hash.fetch('non-existing') # raises error hash.fetch('non-existing', 'default') # 'default' hash.each do |key, value| p "#{key} has value #{value}" end hash.key?(:one) # true hash.include?(‘The One’) # true
  19. Array interactions - Sorting arrays 27 # loops 10.times {

    puts "Hello world" } 10.times { |n| puts "Hello world ##{n}" } 1.upto(10) { |n| puts "Hello world ##{n}" } until fished # do stuff end while !fished # do stuff end loop do # do stuff, same as while true end
  20. But what if… 28 if x == z # do

    this elsif x == y # do that else # PROTIP: never use else! end unless x == y # do this end # same as if x != y # do this end
  21. Objects, Objects, Objects… 29 “foo".class # String 1.class # Integer

    1.class.class # Class 1.class.class.class # Class
  22. You’ve got some class, Ruby! 30 class Greeter def greet(name

    = nil) name ||= ‘World' # Unless nil… (NULL) "Hello #{name}" end end greeter = Greeter.new greeter.greet # Hello World! greeter.greet('Portugal') # Hello Portugal!
  23. You’ve got some class, Ruby! 31 # Monkey patching String

    class String def rock! "#{self} rocks!" end end "ruby".rock! "ruby rocks!" "ruby rocks".capitalize # Ruby rocks "ruby rocks".upcase # RUBY ROCKS "RuBy Rocks".downcase # ruby rocks "ruby rocks".split(' ').map(&:capitalize).join(' ‘) # Ruby Rocks
  24. You’ve got some class, Ruby! 32 class Person attr_reader :name

    attr_reader :age def initialize(name, age) @name = name @age = age end end class FlexiblePerson < Person def name=(new_name) @name = new_name end end someone= Person.new('Bob') someone.name # Bob someone.name = 'someone' # raises error 'undefined method...' someone_else = FlexiblePerson.new('Bob') someone_else = 'Not Bob' someone_else.name # Not Bob
  25. Why Ruby? - Porto, 15/2/2018 Do ruby developers have fun?

    36 0 0,25 0,5 0,75 1 No Yes 100% 0%
  26. Why Ruby? - Porto, 15/2/2018 Do ruby developers have fun?

    Undeniable history of fun Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. It was designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan. It was designed to be fun... 37 No Yes International Institute of Fun
  27. Why Ruby? - Porto, 15/2/2018 39 Machines are cheap Developers

    are expensive Good developers are scarce
  28. 40