From [Revised 2nd Edition] Ruby Certification Exam Study Guide (Silver/Gold)
Can you understand the difference in the following questions?
What happens when the following code is executed?
MAX=10
print MAX
MAX=100
print MAX
What happens when the following code is executed?
HOGE = "hoge"
HOGE.gsub!("hoge", "piyo")
print HOGE
Both questions are familiar syntax problems about constant warnings in the Ruby exam.
10
is displayed first, followed by a warning, and then 100
is displayed.piyo
is displayed.The difference between these two questions lies in whether a destructive method is used.
In Question 2, a destructive method is called, so no warning occurs, and the string is replaced.