Why is there an exclamation point on the Ruby method?

Asked 2 years ago, Updated 2 years ago, 125 views

Do you have this question mark? Are you right? I understand it's a question mark because you ask like this

What is the exclamation mark for?

ruby naming-convention method

2022-09-22 22:00

1 Answers

A method that ends with an exclamation point means, "This method changes the content of the object." It is also called dangerous methods because unexpected things can happen if you are referencing this object elsewhere.

foo = "A STRING"
Foo.downcase! # Turn yourself into a lowercase letter
puts foo

If you look at libaray, you can see that the same name method is divided into two, one with an exclamation point and one without an exclamation point (the one without an exclamation point is called safe methods).

foo = "A STRING"
bar = foo.downcase #foo doesn't change, return
puts foo            # prints unchanged foo
puts bar            # prints newly created bar


2022-09-22 22:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.