advanced patterns when modifying frozen objects in Ruby 3.1
I'm building a feature where I'm migrating some code and I've been banging my head against this for hours... I'm working with an scenario with modifying frozen objects in Ruby 3.1. I have a simple class that uses a frozen array to store values, but when I try to modify the array, I'm getting a `FrozenError`. Here's the code I have: ```ruby class MyClass attr_reader :values def initialize @values = [1, 2, 3].freeze end def add_value(new_value) @values << new_value end end my_instance = MyClass.new my_instance.add_value(4) ``` When I run this code, I get the following behavior: ``` FrozenError: need to modify frozen Array ``` I thought that using `freeze` was a way of preventing modifications to the object, but it seems to be causing issues when I try to add a new value. I’ve tried using `dup` to create a copy of the array before modifying it, like this: ```ruby def add_value(new_value) values_copy = @values.dup values_copy << new_value @values = values_copy end ``` However, this still raises the same behavior. Am I misunderstanding how `freeze` works, or is there a better way to handle state in this scenario? I want to maintain immutability but also need to be able to add values dynamically. Any help would be appreciated! Thanks in advance! For context: I'm using Ruby on Ubuntu 22.04. Is this even possible?