Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up
for free
The Hamster Gem - Ryan Mulligan
Las Vegas Ruby Group
July 16, 2014
1
56
The Hamster Gem - Ryan Mulligan
Las Vegas Ruby Group
July 16, 2014
Tweet
Share
More Decks by Las Vegas Ruby Group
See All by Las Vegas Ruby Group
lvrug
0
77
lvrug
0
25
lvrug
0
50
lvrug
0
25
lvrug
3
75
lvrug
1
61
lvrug
0
22
lvrug
1
34
lvrug
0
25
Featured
See All Featured
iamctodd
17
1.9k
holman
448
130k
keithpitt
401
20k
morganepeng
92
14k
trishagee
20
2.1k
destraynor
223
47k
brad_frost
156
6.4k
ammeep
656
54k
pedronauck
652
110k
mthomps
39
2.3k
addyosmani
494
110k
marcelosomers
220
15k
Transcript
“Efficient, immutable, and thread-safe collection classes for Ruby.” the hamster
gem
“Efficient, immutable, and thread-safe collection classes for Ruby.”
“Efficient, immutable, and thread-safe collection classes for Ruby.”
containers for objects
tuple (1, 2, 3)
set {1, 2, 3}
hash {:name => "James", :gender => :male}
vector (like array) [1, 2, 3, 4, 5]
stack User:Boivie http://en.wikipedia.org/wiki/Stack_(abstract_data_type)#mediaviewer/File:Data_stack.svg
queue User:Vegpuff on wikipedia http://en.wikipedia.org/wiki/Queue_(abstract_data_type)#mediaviewer/File:Data_Queue.svg
(linked) list
“Efficient, immutable, and thread-safe collection classes for Ruby.”
immutability
require "hamster" person = Hamster.hash(:name => "Simon", :gender => :male)
person[:name] # => "Simon" friend = person.put(:name, "James") # => {:name => "James", :gender => :male} male = person.delete(:name) # => {:gender => :male} person # => {:name => "Simon", :gender => :male} male.key?(:name) # => false
the sales pitch
immutability is a good thing™
Mutable stateful objects are the new spaghetti code: - hard
to understand, test, reason about - Concurrency disaster Rich Hickey - Clojure “
Immutable objects are simple. Classes should be immutable unless there’s
a very good reason to make them mutable. If a class cannot be made immutable, limit its mutability as much as possible. Josh Bloch, in Effective Java “
you are using immutable data
git, bitcoin blockchain, :ruby
benefits readability concurrency (thread-safety)
“Efficient, immutable, and thread-safe collection classes for Ruby.”
person = Hamster.hash( :name => "Simon", :gender => :male, #...
1000 other key-value pairs ) person[:name] # => "Simon" friend = person.put(:name, "James") # => {:name => "James", :gender => :male, #...} Does friend have to copy everything from person?
NO!
“Efficient, immutable, and thread-safe collection classes for Ruby.” NO!
“Persistent, immutable, and thread-safe collection classes for Ruby.” NO!
zs = xs + ys
None
adding e
“Efficient, immutable, and thread-safe collection classes for Ruby.”
immutability => thread safety
warning: references immutable but contents might be mutable
require 'hamster' v = "A" h = Hamster.hash(:k=>v) #=>{:k =>
"A"} v[0] = "B" h #=> {:k => "B"}
“Efficient, immutable, and thread-safe collection classes for Ruby.”
references https://deveo.com/blog/2013/03/22/immutability-in-ruby-part-1/ https://deveo.com/blog/2013/03/28/immutability-in-ruby-part-2/ http://clojure.org/state http://en.wikipedia.org/wiki/Persistent_data_structure https://github.com/hamstergem/hamster slides https://github.com/ryantm/lvrug_hamster