shou2017.com
JP

Ruby Certification: Constant Warnings

Wed Jul 18, 2018
Sat Aug 10, 2024

From [Revised 2nd Edition] Ruby Certification Exam Study Guide (Silver/Gold)

Can you understand the difference in the following questions?

Question 1

What happens when the following code is executed?

MAX=10
print MAX
MAX=100
print MAX

Question 2

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.

Answers

  • Question 1: 10 is displayed first, followed by a warning, and then 100 is displayed.
  • Question 2: No warning occurs, and piyo is displayed.

Explanation

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.

See Also