user2439166
user2439166

Reputation:

Interrogative and exclamation methods

When should a method name be interrogative and exclamation? Is there any convention/rules?

Upvotes: 2

Views: 210

Answers (3)

Bijendra
Bijendra

Reputation: 10035

Interrogative - when the method expects boolean values in return.

Exclamation - when the method overwrites the object which is called upon.

Upvotes: 1

Pravin Mishra
Pravin Mishra

Reputation: 8434

  • The question mark on a method signifies that the method returns a boolean result.

  • The exclamation point at the end of a method signifies the method will modify the object that it is called on. Ruby calls these “dangerous” methods because they change the state of the object. A method without an exclamation point is considered a “safe” method because it makes a copy of the object and returns the copy instead of modifying the passed in object.

Upvotes: 3

muttonlamb
muttonlamb

Reputation: 6501

Generally speaking the bang operator (!) is placed on methods that are destructive i.e. they change things. .map is safe, whilst .map! changes the object it is called upon.

Interrogative generally return a boolean response.

Upvotes: 1

Related Questions