Immutability in Ruby

By Lorenzo Barasti

What do we mean with 'immutability'?

In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created

What is it good for?

What is it good for?

  • Testability
  • Predictability
  • Debugging

What is it good for?

  • Testability

- factoring out time dependencies
- running tests concurrently
- avoiding afterAll, beforeAll

What is it good for?

  • Parallelization comes more natural
  • More emphasis on your data-flow
  • Readability (?)
  • Performance (??)

Vector is a collection type [...] that addresses the inefficiency for random access on lists. Vectors allow accessing any element of the list in “effectively” constant time.

A real-world example from Scala

what about downsides?

what about downsides?

  • Immutable and mutable data-structures don't play well together
  • Integration with non-functional frameworks can be challenging
  • When dealing with stateful functions extra care must be taken  


  • An Introduction to immutability in Ruby by valve - probably the best place to start if you are new to the topic
  • Immutability in Ruby - a very thorough article about the value of immutable data structures from Deveo's blog
  • Immutable Ruby - a brilliant talk by Michael Fairley
  • Hamster - a Ruby library that provides "Efficient, immutable, and thread-safe collection classes for Ruby"
  • Scala Vector - documentation page
  • To abstract or not to abstract? - interesting opinion about the value of abstraction vs efficiency by Mike Perham

THANK YOU for Listening!