41
Long Live the Rubyists! Kane Baccigalupi @rubyghetto

Long Live the Rubyist

Embed Size (px)

Citation preview

Page 1: Long Live the Rubyist

Long Live the Rubyists!Kane Baccigalupi

@rubyghetto

Page 2: Long Live the Rubyist

Life After Ruby

Page 3: Long Live the Rubyist

But WHY???

:(

Page 4: Long Live the Rubyist

Ruby is!

• expressive

Page 5: Long Live the Rubyist

“hello ” * 4

“hello hello hello hello ”

Page 6: Long Live the Rubyist

Ruby is!

• expressive

• principle of least surprise

Page 7: Long Live the Rubyist

“hello”.empty?

“hello”.include?(“hell”)

Page 8: Long Live the Rubyist
Page 9: Long Live the Rubyist

Ruby is!

• expressive

• principle of least surprise

• powerful

Page 10: Long Live the Rubyist

class Object

def wat

“wat? wat?”

end

end

“foo”.wat # “wat? wat?”

Page 11: Long Live the Rubyist

Ruby is!

• expressive

• principle of least surprise

• powerful

• libs!

Page 12: Long Live the Rubyist

Rails!

Page 13: Long Live the Rubyist

So what’s

the problem???

Page 14: Long Live the Rubyist
Page 15: Long Live the Rubyist

Oh crap!

Concurrency :(

Page 16: Long Live the Rubyist
Page 17: Long Live the Rubyist

–Tony Arcieri, creator of Celluloid gem

"Eventually you're in a place where

you're trying to build a jet engine out

of silly putty."

Page 18: Long Live the Rubyist

Where’s the goo?

Page 19: Long Live the Rubyist

Where’s the goo?

MUTABLE STATE

Page 20: Long Live the Rubyist

Where’s the goo?

MUTABLE STATE

def cambiarse!

@changed = true

end

Page 21: Long Live the Rubyist

Functional

Programming

to the rescue!

Functional programmers believe functional

programming is about keeping state immutable

Page 22: Long Live the Rubyist

Mutating Ruby:

greeting = “hello"

greeting.upcase!

greeting.gsub!("HELL", “heaven")

greeting # “heavenO”

Page 23: Long Live the Rubyist

Non-Mutating Ruby:

greeting = “hello"

greeting.upcase.gsub("HELL", “heaven")

greeting # “hello”

Page 24: Long Live the Rubyist

So let’s switch to

functional programming

(define checkbook (lambda ()

; This check book balancing program was written to illustrate

; i/o in Scheme. It uses the purely functional part of Scheme.

; These definitions are local to checkbook

(letrec

; These strings are used as prompts

((IB "Enter initial balance: ")

(AT "Enter transaction (- for withdrawal): ")

(FB "Your final balance is: ")

; This function displays a prompt then returns

; a value read.

(prompt-read (lambda (Prompt)

(display Prompt)

(read)))

; This function recursively computes the new

; balance given an initial balance init and

; a new value t. Termination occurs when the

; new value is 0.

(newbal (lambda (Init t)

(if (= t 0)

(list FB Init)

(transaction (+ Init t)))))

; This function prompts for and reads the next

; transaction and passes the information to newbal

(transaction (lambda (Init)

(newbal Init (prompt-read AT)))))

; This is the body of checkbook; it prompts for the

; starting balance

(transaction (prompt-read IB)))))

Page 25: Long Live the Rubyist

Object Oriented

programming to the

rescue!

Object oriented programmers believe object oriented

coding is breaking code into modular abstractions

Page 26: Long Live the Rubyist

Immutable Objects?

Page 27: Long Live the Rubyist

FAUXY!

Page 28: Long Live the Rubyist

FAUXY!Faux Objects

Page 29: Long Live the Rubyist

FAUXY!Faux Functional

Page 30: Long Live the Rubyist

FAUXY!Oops, no logo though

Page 31: Long Live the Rubyist

FAUXY!Oops, no logo though.

And `1 + 1` still doesn’t equal `2`

Page 32: Long Live the Rubyist

LexingBreaking a string of characters into

a string of tokens

1 + 1

<integer 1> <id ‘+’> <integer 1>

Page 33: Long Live the Rubyist

ParsingTaking a string of tokens and

converting it into tree of expressions

<integer 1> <id ‘+’> <integer 1>

(method_call,

(receiver, <integer 1>),

(message, <id ‘+’>),

(arguments, (<integer 1>))

)

Page 34: Long Live the Rubyist

InterpretationTaking the tree expression

and executing it as code!

(method_call,

(receiver, <integer 1>),

(message, <id ‘+’>),

(arguments, (<integer 1>))

)

• Find or build literal integer `1`

• Use Integer class to lookup `+`

method

• Find literal integer `1` again to use

as an argument

• Call `+` method with found literals

`1` and `1` as the receiver and

the message

Page 35: Long Live the Rubyist

FAUXY!What does it look like though?

Page 36: Long Live the Rubyist

FAUXY! - Multiple Dispatch

NumberToWords.Digit: Class.new(n) -> {

to-words: -> { convert(n) }

convert: -> (n: 1) { 'one' }

convert: -> (n: 2) { 'two' }

convert: -> (n: 3) { 'three' }

convert: -> (n: 4) { 'four' }

convert: -> (n: 5) { 'five' }

convert: -> (n: 6) { 'six' }

convert: -> (n: 7) { 'seven' }

convert: -> (n: 8) { 'eight' }

convert: -> (n: 9) { 'nine' }

}

Page 37: Long Live the Rubyist

FAUXY! - Pipes & Streams

a(x) >> b(:foo, _.value) >> c(_, 'bar')

DataParser.new(data) >> DbRecord.new(_).create

Page 38: Long Live the Rubyist

Aggregating State

Spec: Class.new(description, block) -> {

setup: -> {

results: List.new

}

run: -> {

block.run

}

assert: -> (value) {

results.add(value.to_boolean)

}

failure-count: -> {

results.count -> (boolean) { !boolean }

}

success?: -> {

results.count > 1 && failure-count == 0

}

}

Page 39: Long Live the Rubyist

FAUXY!Watch it happen:

github.com/baccigalupi/fauxy

Page 40: Long Live the Rubyist

Attributions

• Ruby icon: www.computersnyou.com

• Confreaks Screenshot of Ernie Miller @rubyconf 2014:

https://www.youtube.com/watch?v=EgjJYkuV0Sc

• Wat: https://www.destroyallsoftware.com/talks/wat

• Rails icon: en.wikipedia.org

• Moore’s Law: www.pcworld.com

• Celluloid: https://celluloid.io/

• Scheme example:

https://classes.soe.ucsc.edu/cmps112/Spring03/languages/scheme/Sche

meTutorialA.html#example

• Immutable Ruby Object Example:

https://github.com/socialchorus/slipcover/blob/master/lib/slipcover/databa

se.rb

• Presentation at: http://www.slideshare.net/baccigalupi/long-live-the-

rubyist

Page 41: Long Live the Rubyist

ME!Kane Baccigalupi

@baccigalupi

Ruby/Rails/JS

Rescue Coding