[RUBY] [Note] Methods ending with?

I will write an article as a substitute for a memo for studying.

Methods ending in?

Ruby method names can end with? By convention, methods ending in? Are methods that return boolean values.

#True if empty string, false otherwise
''.empty?   #=> true
'abc'.empty?   #=> false

#True if the argument string is included, false otherwise
'movie'.include?('mo')   #=> true
'movie'.include?('at')   #=> false

#True if odd, false otherwise
3.odd?   #=> true
4.odd?   #=> false

You can define the methods that end with?

#Returns true if it is a multiple of 2, otherwise false
def multiple_of_two?(n)
  n % 2 == 0
end
multiple_of_two(1) #=> false
multiple_of_two(2) #=> true
multiple_of_two(3) #=> false

Summary

If the method is intended to return a boolean value, it is better to end it with?.

Recommended Posts

[Note] Methods ending with?
Mock static methods with PowerMock
(Note) Java classes / variables / methods
[Note] Scene transition with JavaFX
Study Java with Progate Note 1
Note
[Java] Points to note with Arrays.asList ()
Note
[Ruby] Handle instance variables with instance methods
[Java] Test private methods with JUnit
Mock only some methods with Spock
[Note] How to get started with Rspec