Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Truth value evaluating of an object

Thread Tools

Truth value evaluating of an object

Posts: n/a
I know ruby treat an object as false whenever it is nil or false.
However, I wonder if there are any other ways to change this behavior.

For example, I define a class called AreYouOk.
class AreYouOk
def initialize(ok)
@ok = ok

x =
puts "you are ok" if x

Since x is not nil, ruby prints " you are ok".
However, I want ruby to make the decision based on the @ok instance
variable. Are there any ways to do that?

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?

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
evaluating a object string jman Javascript 4 03-06-2008 07:53 PM
Truth value evaluating of an object Dave River Ruby 4 10-27-2007 11:30 PM
Confused - suggest parentheses around assignment used as truth value Richard Tobin C Programming 6 04-22-2007 10:46 PM
How to validate one control based on the truth value of another validator? ASP .Net 1 10-12-2005 03:56 PM
Java program is not evaluating String value correctly... Shelly Java 8 12-10-2004 04:12 PM