Truth value evaluating of an object

Discussion in 'Ruby' started by Dave River, Oct 27, 2007.

  1. Dave River

    Dave River 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?
    --
    Posted via http://www.ruby-forum.com/.
     
    Dave River, Oct 27, 2007
    #1
    1. Advertising

  2. Dave River

    Dave River Guest


    > Thus 'if x.ok?' returns the same as 'if x' for any object, but @ok for
    > yours!



    Thanks for your explanation!

    In fact, I am making a wrapper class called Boolean which cooperates
    with some legacy code in my company because there are some compatibility
    problems between different languages.

    I write some code like the following and the Boolean object hides some
    underlying code which solve the compatibility problems.
    x = Boolean.new()
    if x
    do something....
    end

    If ruby does not support something like __bool__ in Python, I need to
    write some code in the following way.
    x = Boolean.new()
    if x.evaluate()
    do something...
    end

    But I would prefer "if x " instead of "if x.evaluate" because it is more
    straight forward. So, I would like to know whether there are any ways to
    do so.

    Regards,
    Dave
    --
    Posted via http://www.ruby-forum.com/.
     
    Dave River, Oct 27, 2007
    #2
    1. Advertising

  3. Dave River

    Phrogz Guest

    On Oct 27, 9:18 am, Dave River <> wrote:
    > I write some code like the following and the Boolean object hides some
    > underlying code which solve the compatibility problems.
    > x = Boolean.new()
    > if x
    > do something....
    > end
    >
    > If ruby does not support something like __bool__ in Python, I need to
    > write some code in the following way.
    > x = Boolean.new()
    > if x.evaluate()
    > do something...
    > end
    >
    > But I would prefer "if x " instead of "if x.evaluate" because it is more
    > straight forward. So, I would like to know whether there are any ways to
    > do so.


    There are not any ways to do so in Ruby. Sorry. (This question comes
    up every few weeks or so.)
     
    Phrogz, Oct 27, 2007
    #3
  4. On 27.10.2007 17:18, Dave River wrote:
    >> Thus 'if x.ok?' returns the same as 'if x' for any object, but @ok for
    >> yours!

    >
    >
    > Thanks for your explanation!
    >
    > In fact, I am making a wrapper class called Boolean which cooperates
    > with some legacy code in my company because there are some compatibility
    > problems between different languages.
    >
    > I write some code like the following and the Boolean object hides some
    > underlying code which solve the compatibility problems.
    > x = Boolean.new()
    > if x
    > do something....
    > end
    >
    > If ruby does not support something like __bool__ in Python, I need to
    > write some code in the following way.
    > x = Boolean.new()
    > if x.evaluate()
    > do something...
    > end
    >
    > But I would prefer "if x " instead of "if x.evaluate" because it is more
    > straight forward. So, I would like to know whether there are any ways to
    > do so.


    What exactly does Boolean do? Maybe you can get rid of it or do some
    other changes so you can directly work with "true" and "false.

    Kind regards

    robert
     
    Robert Klemme, Oct 27, 2007
    #4
  5. On Oct 27, 2007, at 10:35 AM, Dave River wrote:

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


    What you are asking about looks to me like a flag class.

    <code>
    class Flag
    def initialize(state=false)
    @state = state
    end
    def set?
    @state
    end
    def set
    @state = true
    end
    def clear
    @state = false
    end
    end

    ok = Flag.new
    puts "you are ok" if ok.set?
    puts "you are not ok" unless ok.set?
    ok.set
    puts "you are ok" if ok.set?
    puts "you are not ok" unless ok.set?

    </code>

    First the string "you are not ok" is printed. After the flag is set,
    the string "you are ok" is printed.

    Regards, Morton
     
    Morton Goldberg, Oct 28, 2007
    #5
    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:
    701
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Oct 12, 2005
  2. Richard Tobin
    Replies:
    6
    Views:
    626
    Richard Tobin
    Apr 22, 2007
  3. Truth value

    , May 13, 2009, in forum: C Programming
    Replies:
    15
    Views:
    913
    Keith Thompson
    May 14, 2009
  4. Byron
    Replies:
    0
    Views:
    99
    Byron
    Oct 28, 2007
  5. kj

    The value of Truth

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

Share This Page