Creating multiple copies of objects in Ruby

Since everything is an object in Ruby having a functionality that can duplicate objects is not a bad idea. Ruby ships with two methods for making copies of an object: the dup method and the clone method. In Ruby, all variables hold a reference to an object. In a case where a section of a code modifies an object that is not meant to be modified, it is ideal to make a copy of that object to be used in that section of the code, protecting the integrity of the copied object....

September 7, 2023 · 2 min · Aleem Isiaka

When to use puts, print, and p in Ruby

Usually, programming languages have methods for printing out variables. Ruby is not an exception. We will explore the 3 popular methods for printing variables in the Ruby Programming language. The print method The way print(var) works is basically converting its value to a string by calling the to_s method on the object(everything is an object in Ruby) before printing the value and returning nil to its caller. num = 123 print(num) # -> 123 => nil The print method can be easily used for concatenating strings...

September 5, 2023 · 2 min · Aleem Isiaka