Updating the theory of mutation
You can bypass this analysis and reduce the mental effort required by simply avoiding mutation.There is also potential for performance benefits, which we will get to later in this article.In these situations, you have a few options: performance.Mutability has its own source of duplication: defensive copying.Most destructive* methods have non-destructive alternatives. Here are a few examples: Whenever you need to write a new class, try to make it immutable.Immutable classes all follow a simple pattern: never reassign or mutate instance variables.This article is about incorporating functional programming concepts into Ruby code, in a pragmatic way – something I call “functional style.” And when I say pragmatic, what I mean is that the code should still mostly look and feel like idiomatic Ruby. The idea is to work the grain of the language, not against it.The code should be easy for any Rubyist to understand – hopefully even easier than what you’re accustomed to.
It should be the default way to write code, and each deviation should require a good reason. I say “pretend” because practically everything in Ruby is mutable by default, so trying to enforce immutability everywhere is painful.
Concurrent access to mutable data usually requires some sort of coordination, such as a mutex or a semaphore.