Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
The Hamster Gem - Ryan Mulligan
Search
Las Vegas Ruby Group
July 16, 2014
1
70
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
Ruby ISO Standard - David Grayson
lvrug
0
92
Windows Automation - Howard Feldman
lvrug
0
37
Separating Your Application from Rails - Brian Hughes
lvrug
0
70
SWIG and Ruby - David Grayson
lvrug
0
39
Practical Object-Oriented Design in Ruby - Charles Jackson
lvrug
3
96
Varnish+Redis - Russ Smith
lvrug
1
73
Lambdas and Pops - Jan Hettich
lvrug
0
38
Making Good Use of Fonts - Russ Smith
lvrug
1
53
Writing Files - Ryan Mulligan
lvrug
0
31
Featured
See All Featured
How STYLIGHT went responsive
nonsquared
92
4.8k
[RailsConf 2023] Rails as a piece of cake
palkan
22
3.9k
Designing with Data
zakiwarfel
95
4.8k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
240
1.2M
Docker and Python
trallard
33
2.7k
Building Applications with DynamoDB
mza
88
5.6k
Building Better People: How to give real-time feedback that sticks.
wjessup
354
18k
How GitHub Uses GitHub to Build GitHub
holman
468
290k
Building a Scalable Design System with Sketch
lauravandoore
455
32k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
34
8.9k
Build The Right Thing And Hit Your Dates
maggiecrowley
23
2k
Code Reviewing Like a Champion
maltzj
513
39k
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