Immutable methods in Ruby
In this article, we’ll look at what imitable methods are and how they work.
This article is divided into the following sections:
- What are immutable methods in ruby
- The logic of immutable methods
- Summary
What are immutable methods in ruby
Methods that return a copy of the object and don’t change its state are called immutalbe methods, or safe methods. Typically, this is the majority of methods that you will see in ruby.
For example, String class has the swapcase method. Let’s see how it works:
a = "string"
puts "Object id of 'a': #{a.object_id}"
puts a.swapcase
puts a
puts "Object id of 'a': #{a.object_id}"produces:
Object id of 'a': 47082269429180
STRING
string
Object id of 'a': 47082269429180
=> nilAs we can see, after we called the swapcase method, we got the result in the form of a string that was written in upper case. However, the object itself wasn’t changed. In the end, we have the same object and the same line written in lower case.
The logic of immutable methods
Let’s look at how immutable methods work in more detail:
Get Alexey Sologub’s stories in your inbox
Join Medium for free to get updates from this writer.
1) inside the method we create a copy of the received object;
2) then we make changes on the copied object;
3) then we return the modified copy of the object
So, the original object stays unchanged. Let’s use knowledge in practice and create an “original” safe method. To copy the object we will use the load and dump methods that are defined in the Marshal module because the standard ruby library doesn’t have native methods for deep copy.
Our code:
class TestObject
attr_accessor :name def deep_clone(object)
Marshal.load(Marshal.dump(object))
end def modify_object
copy = deep_clone(self)
copy.name.swapcase!
copy
end end
In this example we see 2 methods. The deep_clone method will create a copy of the object that will have different object id. Then we will change the copy using swapcase! and return the modified object.
test = TestObject.new
test.name = "Test name"
puts test.modify_object
puts test.nameproduces:
tEST NAME
Test name
=> nilSummary
- methods that don’t change the state of an object are called immutable methods
- most methods in ruby are immutable methods
Original article on my blog | Follow Me on Twitter | Subscribe to my newsletter




