Antonio Barra
Antonio Barra

Reputation: 249

Special method in ruby (question mark)

How do you define a method of verification? The example: include? even? odd?

>> 2.odd?
=> false
>> 6.even?
=> true
>> 

Upvotes: 1

Views: 4864

Answers (2)

steenslag
steenslag

Reputation: 80065

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

Upvotes: 5

Xavier Holt
Xavier Holt

Reputation: 14619

The same way you define any other function - the question mark is part of the function name:

def is_it?
    return (...)
end

Cheers!

Upvotes: 10

Related Questions