Define and change constants in a class
suggest changeclass Message
DEFAULT_MESSAGE = "Hello, world"
def speak(message = nil)
if message
puts message
else
puts DEFAULT_MESSAGE
end
end
end
DEFAULT_MESSAGE can be reassigned from outside the class. Ruby prints a warning (already initialized constant) and then uses the new value:
Message::DEFAULT_MESSAGE = "Hullo, world"
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents