C. Calling upcase without assignment does not change str
D. upcase! method does not exist
Solution
Step 1: Analyze str.upcase call
str.upcase returns an uppercase string but does not change str itself unless assigned.
Step 2: Analyze str.upcase! call
str.upcase! modifies str in place, changing it to uppercase.
Final Answer:
Calling upcase without assignment does not change str -> Option C
Quick Check:
Non-bang needs assignment to change object [OK]
Hint: Non-bang methods need assignment to change variable [OK]
Common Mistakes:
Thinking non-bang changes original string
Believing upcase! does not exist
Assuming puts can't print strings
5. You have an array of strings: words = ["apple", "banana", "Cherry"]. You want to convert all words to lowercase, modifying the original array directly. Which code correctly does this using bang methods?
hard
A. words.map { |w| w.downcase }
B. words.each { |w| w.downcase! }
C. words.each { |w| w.downcase }
D. words.map! { |w| w.downcase! }
Solution
Step 1: Understand requirement to modify original array
We want to change each string inside words directly, not create a new array.
Step 2: Analyze each option
words.map! { |w| w.downcase! } uses map! with downcase!, but downcase! returns nil if no change, causing words to have nils.
words.map { |w| w.downcase } returns a new array, does not modify original.
words.each { |w| w.downcase } calls downcase but does not assign, so no change.
words.each { |w| w.downcase! } uses each with downcase!, modifying each string in place correctly.
Final Answer:
words.each { |w| w.downcase! } -> Option B
Quick Check:
Use each with bang method to modify elements [OK]
Hint: Use each with bang method to change elements in place [OK]
Common Mistakes:
Using map! with downcase! causing nils
Using non-bang methods without assignment
Using map without modifying original array
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.