Truth value evaluating of an object

Discussion in 'Ruby' started by Byron, Oct 28, 2007.

  1. Byron

    Byron Guest

    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
    end
    end

    x = AreYouOk.new(false)
    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?
    Byron, Oct 28, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    697
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Oct 12, 2005
  2. Richard Tobin
    Replies:
    6
    Views:
    623
    Richard Tobin
    Apr 22, 2007
  3. Truth value

    , May 13, 2009, in forum: C Programming
    Replies:
    15
    Views:
    911
    Keith Thompson
    May 14, 2009
  4. Dave River
    Replies:
    4
    Views:
    129
    Morton Goldberg
    Oct 28, 2007
  5. kj

    The value of Truth

    kj, Apr 11, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    108
Loading...

Share This Page