Mutation in Ruby

Mutation-related bugs are some of the most common in the Ruby world. Joël and German explore many of the gotchas related to mutation, how to avoid them, and discuss when mutation might not be necessary at all.


This is a companion discussion topic for the original entry at https://thoughtbot.com/upcase/videos/mutation-in-ruby

I really enjoyed this video and learned a lot about mutation. Thank you for making it!

I have a few questions:

  1. When is it appropriate to mutate an object?
  2. When is it encouraged to mutate an object? Are there examples?

I think in the video I heard it was appropriate to mutate objects when the object’s state is local to the function of the class. In other words, if the object is neither passed in as an argument or a collaborator to the class, then it is permitted to mutate an object because the side effects of the change to the object are arguably minimal – there is not a disastrous ripple effect of change across your application.

Still, I did not hear a the speakers talk about examples where it is encouraged to mutate an object vs creating a new one in memory. There seemed to be a strict preference for pure functions.