Long Live the Rubyist
-
Upload
baccigalupi -
Category
Technology
-
view
237 -
download
0
Transcript of Long Live the Rubyist
Long Live the Rubyists!Kane Baccigalupi
@rubyghetto
Life After Ruby
But WHY???
:(
Ruby is!
• expressive
“hello ” * 4
“hello hello hello hello ”
Ruby is!
• expressive
• principle of least surprise
“hello”.empty?
“hello”.include?(“hell”)
Ruby is!
• expressive
• principle of least surprise
• powerful
class Object
def wat
“wat? wat?”
end
end
“foo”.wat # “wat? wat?”
Ruby is!
• expressive
• principle of least surprise
• powerful
• libs!
Rails!
So what’s
the problem???
Oh crap!
Concurrency :(
–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."
Where’s the goo?
Where’s the goo?
MUTABLE STATE
Where’s the goo?
MUTABLE STATE
def cambiarse!
@changed = true
end
Functional
Programming
to the rescue!
Functional programmers believe functional
programming is about keeping state immutable
Mutating Ruby:
greeting = “hello"
greeting.upcase!
greeting.gsub!("HELL", “heaven")
greeting # “heavenO”
Non-Mutating Ruby:
greeting = “hello"
greeting.upcase.gsub("HELL", “heaven")
greeting # “hello”
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)))))
Object Oriented
programming to the
rescue!
Object oriented programmers believe object oriented
coding is breaking code into modular abstractions
Immutable Objects?
FAUXY!
FAUXY!Faux Objects
FAUXY!Faux Functional
FAUXY!Oops, no logo though
FAUXY!Oops, no logo though.
And `1 + 1` still doesn’t equal `2`
LexingBreaking a string of characters into
a string of tokens
1 + 1
<integer 1> <id ‘+’> <integer 1>
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>))
)
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
FAUXY!What does it look like though?
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' }
}
FAUXY! - Pipes & Streams
a(x) >> b(:foo, _.value) >> c(_, 'bar')
DataParser.new(data) >> DbRecord.new(_).create
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
}
}
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
ME!Kane Baccigalupi
@baccigalupi
Ruby/Rails/JS
Rescue Coding