Duplicating Objects in Ruby by Leveraging Symbols


In this guide you’ll walk through how to leverage symbols in Ruby in order to duplicate objects, including how they can share an object_id.


Implement a method that clones a string so that they use the same object_id.

Exercise File

Code File

Exercise Description

Given an array of strings, normally each string would have its own object_id. In this exercise you’ll need to figure out how to convert each element so that they share the same object_id (assuming that the strings are equal).

Example Input

str_array = ['some word', 'some word', 'some word']
cloned_array = duplicate_objects str_array

cloned_array.first.object_id  # => 70265442975020
cloned_array.last.object_id   # => 70265442975020

Real World Usage

This may seem like an odd topic to cover. Enabling strings to have the same object_id typically isn’t something that you will work on regularly. However, there are times when you will be asked to work with symbols, and understanding how they work is critical. And that is what you will learn in this guide.


Can be found on the solutions branch on github.


Please enter your comment!
Please enter your name here