Long Live the Rubyist
-
Upload
baccigalupi -
Category
Technology
-
view
229 -
download
0
Transcript of Long Live the Rubyist
![Page 1: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/1.jpg)
Long Live the Rubyists!Kane Baccigalupi
@rubyghetto
![Page 2: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/2.jpg)
Life After Ruby
![Page 3: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/3.jpg)
But WHY???
:(
![Page 4: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/4.jpg)
Ruby is!
• expressive
![Page 5: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/5.jpg)
“hello ” * 4
“hello hello hello hello ”
![Page 6: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/6.jpg)
Ruby is!
• expressive
• principle of least surprise
![Page 7: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/7.jpg)
“hello”.empty?
“hello”.include?(“hell”)
![Page 8: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/8.jpg)
![Page 9: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/9.jpg)
Ruby is!
• expressive
• principle of least surprise
• powerful
![Page 10: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/10.jpg)
class Object
def wat
“wat? wat?”
end
end
“foo”.wat # “wat? wat?”
![Page 11: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/11.jpg)
Ruby is!
• expressive
• principle of least surprise
• powerful
• libs!
![Page 12: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/12.jpg)
Rails!
![Page 13: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/13.jpg)
So what’s
the problem???
![Page 14: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/14.jpg)
![Page 15: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/15.jpg)
Oh crap!
Concurrency :(
![Page 16: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/16.jpg)
![Page 17: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/17.jpg)
–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](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/18.jpg)
Where’s the goo?
![Page 19: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/19.jpg)
Where’s the goo?
MUTABLE STATE
![Page 20: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/20.jpg)
Where’s the goo?
MUTABLE STATE
def cambiarse!
@changed = true
end
![Page 21: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/21.jpg)
Functional
Programming
to the rescue!
Functional programmers believe functional
programming is about keeping state immutable
![Page 22: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/22.jpg)
Mutating Ruby:
greeting = “hello"
greeting.upcase!
greeting.gsub!("HELL", “heaven")
greeting # “heavenO”
![Page 23: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/23.jpg)
Non-Mutating Ruby:
greeting = “hello"
greeting.upcase.gsub("HELL", “heaven")
greeting # “hello”
![Page 24: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/24.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/25.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/26.jpg)
Immutable Objects?
![Page 27: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/27.jpg)
FAUXY!
![Page 28: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/28.jpg)
FAUXY!Faux Objects
![Page 29: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/29.jpg)
FAUXY!Faux Functional
![Page 30: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/30.jpg)
FAUXY!Oops, no logo though
![Page 31: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/31.jpg)
FAUXY!Oops, no logo though.
And `1 + 1` still doesn’t equal `2`
![Page 32: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/32.jpg)
LexingBreaking a string of characters into
a string of tokens
1 + 1
<integer 1> <id ‘+’> <integer 1>
![Page 33: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/33.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/34.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/35.jpg)
FAUXY!What does it look like though?
![Page 36: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/36.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/37.jpg)
FAUXY! - Pipes & Streams
a(x) >> b(:foo, _.value) >> c(_, 'bar')
DataParser.new(data) >> DbRecord.new(_).create
![Page 38: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/38.jpg)
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 40: Long Live the Rubyist](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/40.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022052509/55aae6421a28ab8a1b8b4880/html5/thumbnails/41.jpg)
ME!Kane Baccigalupi
@baccigalupi
Ruby/Rails/JS
Rescue Coding