Slide 1

Slide 1 text

Why Ruby? Hello World Conf Porto, 15/02/2018

Slide 2

Slide 2 text

Why Ruby? - Porto, 15/2/2018 2 Warning You are about to see inconsistent and poorly designed slides

Slide 3

Slide 3 text

Why Ruby? Old Stuff - Java - PHP - Python - Ruby New stuff - Elixir - Erlang - Javascript - GO - Node.js - Functional Programming Too many choices!

Slide 4

Slide 4 text

Why Ruby? - It is slow! - It won’t scale!

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Why Ruby? - Porto, 15/2/2018 6 Innovative community

Slide 7

Slide 7 text

Why Ruby? - Porto, 15/2/2018 7 IRB

Slide 8

Slide 8 text

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!

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Why Ruby? - Porto, 15/2/2018 10 # deploy to your configured servers $ cap production deploy

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Why Ruby? - Porto, 15/2/2018 13 “Look how much code I am NOT writing”

Slide 14

Slide 14 text

Why Ruby? - Porto, 15/2/2018 14 Mind your own business!

Slide 15

Slide 15 text

15

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Why Ruby? - Porto, 15/2/2018 18 It is fun! =D

Slide 19

Slide 19 text

Printing to the standard output 19 // PHP echo “Hello World" # RUBY puts “Hello World"

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Arrays 21 // PHP in_array(5, $numbers); # RUBY numbers.include?(5)

Slide 22

Slide 22 text

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" }

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

Objects, Objects, Objects… 29 “foo".class # String 1.class # Integer 1.class.class # Class 1.class.class.class # Class

Slide 30

Slide 30 text

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!

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

33 I am having so much fun! Ruby inside

Slide 34

Slide 34 text

34 They tried to tell but I did not listen!

Slide 35

Slide 35 text

35 I hate XML configuration files!

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

Why Ruby? - Porto, 15/2/2018 38 You can make money with it!

Slide 39

Slide 39 text

Why Ruby? - Porto, 15/2/2018 39 Machines are cheap Developers are expensive Good developers are scarce

Slide 40

Slide 40 text

40

Slide 41

Slide 41 text

Why Ruby? - Porto, 15/2/2018 41 We are hiring! https://corporate.xing.com/en/career/ https://landing.jobs/at/xing-pt

Slide 42

Slide 42 text

Why Ruby? - Porto, 15/2/2018 What now? 42 www.codeschool.com/learn/ruby guides.rubyonrails.org Railscasts.com www.driftingruby.com Google

Slide 43

Slide 43 text

43 Write your next pet project with it

Slide 44

Slide 44 text

Questions? https://www.xing.com/profile/Marcelo_Jacobus https://github.com/mjacobus @marcelo_jacobus www.xing.com

Slide 45

Slide 45 text

Thank you for your attention. www.xing.com