Slide 1

Slide 1 text

RUBY FOR JAVA MINDS

Slide 2

Slide 2 text

$ whoami

Slide 3

Slide 3 text

@janogonzalez

Slide 4

Slide 4 text

@hop_in

Slide 5

Slide 5 text

HISTORY

Slide 6

Slide 6 text

THE 90’S

Slide 7

Slide 7 text

BIG CHANGES

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

COLD WAR IS OVER

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

GRUNGE GOES MAINSTREAM

Slide 12

Slide 12 text

APATHY

Slide 13

Slide 13 text

SELF- ALIENATION

Slide 14

Slide 14 text

ANGST IS THE NEW COOL

Slide 15

Slide 15 text

MEANWHILE IN THE PROGRAMMING WORLD...

Slide 16

Slide 16 text

RAISING AGAINST THE MAINSTREAM

Slide 17

Slide 17 text

JAMES GOSLING

Slide 18

Slide 18 text

JAMES GOSLING

Slide 19

Slide 19 text

JAVA

Slide 20

Slide 20 text

“Write Once, Run Anywhere”

Slide 21

Slide 21 text

THE 5 PRINCIPLES • It should be "simple, object-oriented and familiar" • It should be "robust and secure" • It should be "architecture-neutral and portable" • It should execute with "high performance" • It should be "interpreted, threaded, and dynamic"

Slide 22

Slide 22 text

(THAT WAS NOT RSPEC)

Slide 23

Slide 23 text

MATZ

Slide 24

Slide 24 text

MATZ

Slide 25

Slide 25 text

RUBY

Slide 26

Slide 26 text

“Ruby is designed to make programmers happy”

Slide 27

Slide 27 text

MATZ WINS THE NOBEL PEACE PRICE

Slide 28

Slide 28 text

(HISTORICAL ACCURACY IS OVERRATED)

Slide 29

Slide 29 text

2 DIFFERENT PHILOSOPHIES

Slide 30

Slide 30 text

HOW DO WE EMBRACE RUBY?

Slide 31

Slide 31 text

MY HISTORY

Slide 32

Slide 32 text

Y2K

Slide 33

Slide 33 text

COMPUTER APOCALYPSE DID NOT HAPPEN

Slide 34

Slide 34 text

I WENT TO A “JAVA SCHOOL”

Slide 35

Slide 35 text

JAVA EE, SPRING AND CUBICLES

Slide 36

Slide 36 text

HEY, RAILS LOOK COOL!

Slide 37

Slide 37 text

HEY, THERE IS A FLY OVER THERE!

Slide 38

Slide 38 text

(STUPID FLY)

Slide 39

Slide 39 text

THE 10’S

Slide 40

Slide 40 text

RAILS VS JAVA EE WAS OVER

Slide 41

Slide 41 text

HEY, RUBY LOOKS COOL!

Slide 42

Slide 42 text

WAIT, HOW DO I DECLARE AN INTERFACE?

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

THE PROBLEM

Slide 45

Slide 45 text

“The determined Real Programmer can write FORTRAN programs in any language.” –Ed Post

Slide 46

Slide 46 text

HOW TO WRITE RUBY THAT FEELS LIKE RUBY?

Slide 47

Slide 47 text

A ZEN MOMENT

Slide 48

Slide 48 text

ॳ৺

Slide 49

Slide 49 text

BEGINNER’S MIND

Slide 50

Slide 50 text

EMPTY YOUR CUP

Slide 51

Slide 51 text

BACK TO BASICS

Slide 52

Slide 52 text

THE KEY TO UNDERSTAND RUBY

Slide 53

Slide 53 text

EXPRESSIONS + OBJECTS

Slide 54

Slide 54 text

SYNTAX IS EXPRESSION-ORIENTED

Slide 55

Slide 55 text

“LISP programmers know the value of everything and the cost of nothing.” - Alan Perlis

Slide 56

Slide 56 text

EXPRESSIONS def average(a, b) (a + b) / 2 end # => nil average(10, 2) # => 6

Slide 57

Slide 57 text

EXPRESSIONS level = case when 1..2 then :excellent when 3..5 then :normal else :bad end

Slide 58

Slide 58 text

ALL VALUES ARE OBJECTS

Slide 59

Slide 59 text

OBJECTS -1.class # => Fixnum

Slide 60

Slide 60 text

OBJECTS String.object_id # => 70138854779960

Slide 61

Slide 61 text

OBJECTS nil.nil? # => true

Slide 62

Slide 62 text

WO IST DEIN GOTT JETZT

Slide 63

Slide 63 text

MESSAGES

Slide 64

Slide 64 text

MESSAGES EVERYWHERE

Slide 65

Slide 65 text

MESSAGES 40 + 2 # => 42

Slide 66

Slide 66 text

MESSAGES 40.+(2) # => 42

Slide 67

Slide 67 text

MESSAGES 40.send(:+, 2) # => 42

Slide 68

Slide 68 text

MESSAGES ≠ METHODS

Slide 69

Slide 69 text

MESSAGES class AccountProtectionProxy def initialize(real_account, owner) @subject = real_account @owner = owner end ...

Slide 70

Slide 70 text

MESSAGES class AccountProtectionProxy ... def method_missing(name, *args) check_access @subject.send(name, *args) end ... end

Slide 71

Slide 71 text

DYNAMIC TYPING

Slide 72

Slide 72 text

FORGET ABOUT JAVA INTERFACES

Slide 73

Slide 73 text

JAVA public interface Report { ReportFile generate(); }

Slide 74

Slide 74 text

JAVA public class PDFReport implements Report { .... }

Slide 75

Slide 75 text

JAVA public class CSVReport implements Report { .... }

Slide 76

Slide 76 text

JAVA public void print(List reports) { if (reports == null) { return; } for (Report r : reports) { ReportFile file = r.generate(); ... } }

Slide 77

Slide 77 text

DYNAMIC TYPING def print(reports) [*reports].each do |r| file = r.generate ... end end

Slide 78

Slide 78 text

DYNAMIC TYPING class PDFReport def generate ... end ... end

Slide 79

Slide 79 text

DYNAMIC TYPING class CVSReport def generate ... end ... end

Slide 80

Slide 80 text

DYNAMIC TYPING class Duck def generate ... end ... end

Slide 81

Slide 81 text

PROGRAM TO INTERFACES, NOT TO interface

Slide 82

Slide 82 text

USED EVERYWHERE

Slide 83

Slide 83 text

DYNAMIC TYPING "Jano in " << "Berlin" # => "Jano in Berlin" ["Santiago"] << "Berlin" # => ["Santiago", "Berlin"]

Slide 84

Slide 84 text

IF YOU REALLY NEED TO CHECK...

Slide 85

Slide 85 text

DYNAMIC TYPING if report.respond_to?(:generate) report.generate ... else ... end

Slide 86

Slide 86 text

IF YOU REALLY, REALLY NEED TO CHECK...

Slide 87

Slide 87 text

DYNAMIC TYPING if report.is_a?(Report) report.generate ... else ... end

Slide 88

Slide 88 text

DYNAMIC BUT NOT WEAK

Slide 89

Slide 89 text

STRONG TYPING 100 + 'cool' # TypeError: String can't be coerced into Fixnum

Slide 90

Slide 90 text

TYPE CONVERSIONS

Slide 91

Slide 91 text

CONVERSIONS 100 + 'cool'.to_i # => 100 100 + Integer('cool') # ArgumentError: invalid value for Integer(): "lala"

Slide 92

Slide 92 text

BLOCKS

Slide 93

Slide 93 text

BLOCKS ARE USED & ABUSED IN RUBY

Slide 94

Slide 94 text

PROGRAM LIKE A BOSS

Slide 95

Slide 95 text

(LITERALLY)

Slide 96

Slide 96 text

BLOCKS (1..100).select { |n| n % 3 == 0 } .map { |n| n * 2 } .reduce(:+)

Slide 97

Slide 97 text

FLUID CODE

Slide 98

Slide 98 text

BLOCKS File.open('my.txt').each do |line| puts line if line =~ /jano/ end

Slide 99

Slide 99 text

BE MORE DECLARATIVE

Slide 100

Slide 100 text

BLOCKS words.sort do |a, b| a.length <=> b.length end words.min_by? { |w| w.length } words.reject { |w| w.length > 8 } # etc...

Slide 101

Slide 101 text

EXAMPLE: SELF YIELD

Slide 102

Slide 102 text

BLOCKS class Job def initialize yield self if block_given? end ... end

Slide 103

Slide 103 text

BLOCKS job = Job.new do |j| j.name = 'Print Reports' j.user = 'janogonzalez' ... end

Slide 104

Slide 104 text

EXAMPLE: CALLBACKS

Slide 105

Slide 105 text

BLOCKS class Job def on_finish(&block) @end_callback = block end def execute() ... @end_callback.call(self) if @end_callback end end

Slide 106

Slide 106 text

BLOCKS job.on_finish do |j| puts "Finished job #{j.name}" puts "Status: #{j.status}” end

Slide 107

Slide 107 text

MIXINS

Slide 108

Slide 108 text

MODULES AS MIXINS

Slide 109

Slide 109 text

ADD BEHAVIOR TO INSTANCES

Slide 110

Slide 110 text

MODULES module Model def persist ... end end

Slide 111

Slide 111 text

MODULES class User include Model ... end

Slide 112

Slide 112 text

MODULES job = User.new('Jano') job.persist

Slide 113

Slide 113 text

ADD BEHAVIOR TO CLASSES

Slide 114

Slide 114 text

MODULES module Findable def find_by_id(id) ... end end

Slide 115

Slide 115 text

MODULES class User extends Findable ... end

Slide 116

Slide 116 text

MODULES user = User.find_by_id(3)

Slide 117

Slide 117 text

METAPROGRAMMING

Slide 118

Slide 118 text

(USE WITH CAUTION)

Slide 119

Slide 119 text

(AKA THE EVAL IS EVIL RULE)

Slide 120

Slide 120 text

ACCESSORS

Slide 121

Slide 121 text

ACCESSORS class User attr_reader :name attr_reader :status end

Slide 122

Slide 122 text

OPEN CLASSES

Slide 123

Slide 123 text

OPEN CLASSES class Range def even select(&:even?) end end (0..10).even # => [2, 4, 6, 8, 10]

Slide 124

Slide 124 text

OPEN CLASSES class BuggyClass def buggy_method # the fix ... end end

Slide 125

Slide 125 text

OPEN CLASSES class Job alias_method :old_execute, :execute def execute ... old_execute end end

Slide 126

Slide 126 text

DEFINE METHODS

Slide 127

Slide 127 text

DEFINE METHODS obj = User.new User.class_eval do def represent ... end end obj.represent

Slide 128

Slide 128 text

DEFINE METHODS User.instance_eval do def random ... end end User.random

Slide 129

Slide 129 text

SINGLETON METHODS

Slide 130

Slide 130 text

SINGLETON METHOD obj = Person.new class << obj def represent ... end end obj.represent

Slide 131

Slide 131 text

(A LOT MORE TO EXPLORE...)

Slide 132

Slide 132 text

ONE LAST ADVICE

Slide 133

Slide 133 text

कഁ཭

Slide 134

Slide 134 text

SHU कഁ཭

Slide 135

Slide 135 text

HA कഁ཭

Slide 136

Slide 136 text

RI कഁ཭

Slide 137

Slide 137 text

No content

Slide 138

Slide 138 text

CONCLUSIONS

Slide 139

Slide 139 text

EMBRACE DYNAMIC TYPING

Slide 140

Slide 140 text

BE MORE DECLARATIVE

Slide 141

Slide 141 text

WRITE LESS DO MORE

Slide 142

Slide 142 text

CONVENTIONS AND IDIOMS ARE NOT LAWS

Slide 143

Slide 143 text

THERE IS NO TRUE PATH

Slide 144

Slide 144 text

ENJOY RUBY! (AND JAVA TOO)

Slide 145

Slide 145 text

DANKESCHÖN!

Slide 146

Slide 146 text

¡MUCHAS GRACIAS!

Slide 147

Slide 147 text

IMAGES • http://006.shanbara.jp/movie/data/fat1367385194099.jpg • http://wfiles.brothersoft.com/n/nirvana-desktop_157301-1600x1200.jpg • http://stilestili.com/wp-content/uploads/2013/04/Ryan- Gosling2-2048x2560.jpg • http://upload.wikimedia.org/wikipedia/commons/thumb/1/14/ James_Gosling_2008.jpg/1018px-James_Gosling_2008.jpg • http://www.flickr.com/photos/john_lam/1910968816/