From: Arlen Christian Mart Cuss Date: 2007-10-27T23:50:14+09:00 Subject: Re: Truth value evaluating of an object Hi, On Sat, 2007-10-27 at 23:35 +0900, Dave River wrote: > I know that there is a method called __bool__ in Python. You can define > your __bool__ method in your class. The truth value of an object is > based on the return value of __bool__. Does ruby provide similar > mechanism? I don't think I'm speaking out of school when I say that there's no such way to do so. Ruby and libraries likely rely on 'if obj' returning false if it's false or nil - overriding this behaviour will probably cause some strange behaviour which may result in your object being inadvertently modified! Instead, just make an accessor like ok?, so you can test "if x.ok?". If you need some way to test this across several objects, consider this: class Object def ok? self end end class AreYouOk def ok? @ok end end Thus 'if x.ok?' returns the same as 'if x' for any object, but @ok for yours! Cheers Arlen