×
Copy
Open
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
A Dangerous Game ! Safety in Ruby
Slide 2
Slide 2 text
Ryan Levick @itchyankles
Slide 3
Slide 3 text
No content
Slide 4
Slide 4 text
No content
Slide 5
Slide 5 text
No content
Slide 6
Slide 6 text
No content
Slide 7
Slide 7 text
Readability Expressivity Flexibility
Slide 8
Slide 8 text
No content
Slide 9
Slide 9 text
I thought Ruby was perfect
Slide 10
Slide 10 text
But: no one is
Slide 11
Slide 11 text
No content
Slide 12
Slide 12 text
No content
Slide 13
Slide 13 text
No content
Slide 14
Slide 14 text
What else does Ruby lack?
Slide 15
Slide 15 text
def add1(x) x + 1 end
Slide 16
Slide 16 text
def add1(x) x = 0 if x.nil? x + 1 end
Slide 17
Slide 17 text
def add1(x) unless x.respond_to?(:+) raise ArgumentError end ! x + 1 end
Slide 18
Slide 18 text
No content
Slide 19
Slide 19 text
No content
Slide 20
Slide 20 text
So how do we fix this ?!
Slide 21
Slide 21 text
If only there was an automated way to find these errors…
Slide 22
Slide 22 text
Static Typing
Slide 23
Slide 23 text
No content
Slide 24
Slide 24 text
No content
Slide 25
Slide 25 text
int add1(int x) { int answer = x + 1; return answer; }
Slide 26
Slide 26 text
Type Inference
Slide 27
Slide 27 text
add1 x = x + 1
Slide 28
Slide 28 text
So what does it get us?
Slide 29
Slide 29 text
def decorate_user(user) UserDecorator.new(user).decorate end ! user = User.find_by(email: "
[email protected]
") ! decorated_user = decorate_user(user) ! respond_with user
Slide 30
Slide 30 text
undefined method `name' for nil:NilClass from user_decorator.rb:30:in `decorate_user'
Slide 31
Slide 31 text
Option Type
Slide 32
Slide 32 text
Option[Int] : Some(5) None
Slide 33
Slide 33 text
def decorate_user(user) UserDecorator.new(user).decorate end ! user = User.find_by(email: "
[email protected]
") ! decorated_user = decorate_user(user) ! respond_with user
Slide 34
Slide 34 text
def decorateUser(user: User) = { UserDecorator(user).decorate } ! val user = User.findBy(email = "
[email protected]
") ! val decoratedUser = decorateUser(user) ! respondWith user
Slide 35
Slide 35 text
[error]User.scala:24: type mismatch; [error] found Option[User]: [error] required: User [error] val decoratedUser = decorateUser(user)
Slide 36
Slide 36 text
“undefined method `foo' for nil:NilClass” No more!
Slide 37
Slide 37 text
Wait
Slide 38
Slide 38 text
Isn’t this also more intention revealing?
Slide 39
Slide 39 text
find(id: int) : User * may return nil
Slide 40
Slide 40 text
find(id: int) : Option[User]
Slide 41
Slide 41 text
Redis.fetch(key: String): String * may throw exception
Slide 42
Slide 42 text
Redis.fetch(key: String): Either[Error, String]
Slide 43
Slide 43 text
Wait Wait
Slide 44
Slide 44 text
Isn’t this what tests are for?
Slide 45
Slide 45 text
What is the best testing library ever invented?
Slide 46
Slide 46 text
No content
Slide 47
Slide 47 text
No content
Slide 48
Slide 48 text
No content
Slide 49
Slide 49 text
Math
Slide 50
Slide 50 text
No content
Slide 51
Slide 51 text
For all inputs a valid and correct output is given
Slide 52
Slide 52 text
Always present and cannot be forgotten
Slide 53
Slide 53 text
Cannot be erased
Slide 54
Slide 54 text
The compiler is far less likely to be buggy than your tests
Slide 55
Slide 55 text
Test Business Logic
Slide 56
Slide 56 text
No content
Slide 57
Slide 57 text
def head(list) list.first end
Slide 58
Slide 58 text
def head(list) if list.empty? raise ArgumentError end ! list.first end
Slide 59
Slide 59 text
def head(list) head = list.first MyRubyOptionType.new(head) end
Slide 60
Slide 60 text
Idris
Slide 61
Slide 61 text
head : Vect (S n) a -> a head (h::tail) = h
Slide 62
Slide 62 text
head : Vect (S n) a -> a head (h::tail) = h
Slide 63
Slide 63 text
head : Vect (S n) a -> a head (h::tail) = h
Slide 64
Slide 64 text
head : Vect (S n) a -> a head (h::tail) = h
Slide 65
Slide 65 text
head : Vect (S n) a -> a head (h::tail) = h
Slide 66
Slide 66 text
head : Vect (S n) a -> a head (h::tail) = h
Slide 67
Slide 67 text
head : Vect (S n) a -> a head (h::tail) = h
Slide 68
Slide 68 text
x,y = [1,2]
Slide 69
Slide 69 text
head : Vect (S n) a -> a head (h::tail) = h
Slide 70
Slide 70 text
Idris> head [] Can’t unify Vect 0 a with Vect (S n) iType
Slide 71
Slide 71 text
No more runtime errors!
Slide 72
Slide 72 text
No content
Slide 73
Slide 73 text
class Foo typesig("self.build: (Hash) -> Post") def self.build(attrs) # ... method definition end end
Slide 74
Slide 74 text
No content
Slide 75
Slide 75 text
But is that what we want?
Slide 76
Slide 76 text
Ruby is Dynamic and that’s great!
Slide 77
Slide 77 text
Ruby is Dynamic and that’s great!
Slide 78
Slide 78 text
But…
Slide 79
Slide 79 text
No content
Slide 80
Slide 80 text
No content
Slide 81
Slide 81 text
Zen and the Art of Motorcycle Maintenance
Slide 82
Slide 82 text
Value Rigidity
Slide 83
Slide 83 text
We should know what we’re giving up.
Slide 84
Slide 84 text
Thank you! @itchyankles