Are your objects truly the same, or just pretending to be?
Object identity (equal? vs ==) in Ruby - When to Use Which
Imagine you have two boxes that look exactly the same and contain the same toys. You want to check if they are the same box or just two boxes with the same toys.
Manually checking if two things are exactly the same object or just look alike is confusing and easy to get wrong. You might think two things are equal because they look the same, but they could be different objects in memory.
Ruby gives you two ways to compare: == checks if two objects look equal, while equal? checks if they are the exact same object. This helps you avoid mistakes and understand your program better.
if a == b puts 'Looks equal' end if a.equal?(b) puts 'Same object' end
puts 'Equal in value' if a == b puts 'Identical object' if a.equal?(b)
This concept lets you clearly tell when two things are just alike or truly the same, helping you write smarter and bug-free code.
When you copy a list of friends, you want to know if you have a new list with the same names or just another name tag pointing to the original list.
== checks if objects look equal.
equal? checks if objects are the exact same.
Knowing the difference helps avoid bugs and confusion.