How to Create an Immutable Array in Ruby

1
603

In this coding exercise, you’ll learn how to create an immutable array in Ruby.

Summary

Create an immutable array in Ruby.

Exercise File

Code File

Exercise Description

Given the following array:

[1, 2, 3]

Update the array so that it cannot be changed. If an attempt is made to alter the array, it should raise a runtime error

Example Process

[1, 2, 3] << 4
# ~> RuntimeError: can't modify frozen Array

Real World Usage

Converting an array to be immutable is a process utilized on a daily basis in data science. Since it’s vital to be confident that collection data will be consistent, this feature is very important for building big data algorithms.

Solution

Can be found on the solutions branch on github.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here