March 5 - 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.
Guide Tasks
- Read Tutorial
- Watch Guide Video
- Complete the Exercise
Video locked
This video is viewable to users with a Bottega Bootcamp license
Already a Bottega Student? Sign In
Summary
Implement a method that clones a string so that they use the same object_id.
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.