Special method in ruby (question mark)
How do you define a method of verification? The example: include? even? odd?
>> 2.odd?
=> f开发者_开发百科alse
>> 6.even?
=> true
>>
The same way you define any other function - the question mark is part of the function name:
def is_it?
return (...)
end
Cheers!
class Integer
def is_even?
self.remainder(2) == 0
# this is either true or false, just what we want
end
end
p 4.is_even?
#=> true
精彩评论