Ruby - if statement?

In Ruby, the if statement looks like this val = 1 if val == 1 p "Equality Checked!" end And for if else val = 2 if val == 1 p "Equality Checked!" else p "Equality Unchecked!" end And for if, else if, else val = 2 if val == 1 p "Equality Checked!" elsif val == 2 p "Equality Middle Checked!" else p "Equality Unchecked!" end Also, remember that everything in ruby returns a value, so your if statement can return a value that could be stored in another variable....

January 15, 2024 · 1 min · Aleem Isiaka

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