开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜