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
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
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
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!
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