0
0
Rubyprogramming~10 mins

Object identity (equal? vs ==) in Ruby - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if two strings have the same content using the correct operator.

Ruby
str1 = "hello"
str2 = "hello"
puts str1 [1] str2
Drag options to blanks, or click blank then click option'
A==
Bequal?
C===
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using equal? instead of == to compare content.
2fill in blank
medium

Complete the code to check if two variables point to the exact same object.

Ruby
a = "ruby"
b = a
puts a.[1](b)
Drag options to blanks, or click blank then click option'
A==
B===
Cequal?
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of equal? to check object identity.
3fill in blank
hard

Fix the error in the code to correctly check if two arrays are the same object.

Ruby
arr1 = [1, 2, 3]
arr2 = [1, 2, 3]
puts arr1.[1](arr2)
Drag options to blanks, or click blank then click option'
A==
B===
C!=
Dequal?
Attempts:
3 left
💡 Hint
Common Mistakes
Using == which returns true for arrays with same content but different objects.
4fill in blank
hard

Fill both blanks to create a hash that maps strings to their object identity check results.

Ruby
str1 = "test"
str2 = "test"
result = { str1: str1 [1] str2, str2: str1.[2](str2) }
Drag options to blanks, or click blank then click option'
A==
Bequal?
C===
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up == and equal? in the hash values.
5fill in blank
hard

Fill all three blanks to create a method that returns true if two objects have the same content but are different objects.

Ruby
def same_content_different_object?(obj1, obj2)
  obj1 [1] obj2 && !obj1.[2](obj2) && obj1 [3] obj2
end
Drag options to blanks, or click blank then click option'
A==
Bequal?
Cis_a?
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using equal? instead of == for content comparison.