Sagewire Logo

Truth value evaluating of an object

6 Message(s) by 5 Author(s) originally posted in ruby programming


From: Dave River Date:   Saturday, October 27, 2007
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)
@xxxxxxxxxxx = ok
end
end

x = AreYouOk.new(false)
puts "you are ok" if x

Since x isn't nil, ruby prints " you are ok".
However, I want ruby to make the decision based on the @xxxxxxxxxxx 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/.


From: Arlen Christian Mart Cuss Date:   Saturday, October 27, 2007
Hi,

wrote in message:
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 do not 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?
@xxxxxxxxxxx
end
end

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

Cheers
Arlen


From: Dave River Date:   Saturday, October 27, 2007
Thus 'if x.ok?' returns the same as 'if x' for any object, but @xxxxxxxxxxx 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 doesn't 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'd prefer "if x " instead of "if x.evaluate" because it is more
straight forward. So, I'd like to know whether there are any ways to
do so.

Regards,
Dave
--
Posted via http://www.ruby-forum.com/.


From: Phrogz Date:   Saturday, October 27, 2007
wrote in message:
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 doesn't 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'd prefer "if x " instead of "if x.evaluate" because it is more
straight forward. So, I'd like to know whether there are any ways to
do so.



There aren't any ways to do so in Ruby. Sorry. (This question comes
up every few weeks or so.)


From: Robert Klemme Date:   Saturday, October 27, 2007
wrote in message:
Thus 'if x.ok?' returns the same as 'if x' for any object, but @xxxxxxxxxxx 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 doesn't 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'd prefer "if x " instead of "if x.evaluate" because it is more
straight forward. So, I'd 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


From: Morton Goldberg Date:   Saturday, October 27, 2007
wrote in message:

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)
@xxxxxxxxxxx = ok
end
end
x = AreYouOk.new(false)
puts "you are ok" if x
Since x isn't nil, ruby prints " you are ok".
However, I want ruby to make the decision based on the @xxxxxxxxxxx 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)
@xxxxxxxxxxx = state
end
def set?
@xxxxxxxxxxx
end
def set
@xxxxxxxxxxx = true
end
def clear
@xxxxxxxxxxx = false
end
end

ok = Flag.new
puts "you are ok" if ok.set?
puts "you aren't ok" unless ok.set?
ok.set
puts "you are ok" if ok.set?
puts "you aren't ok" unless ok.set?

</code>

First the string "you aren't ok" is printed. After the flag is set,
the string "you are ok" is printed.

Regards, Morton



Next Message: Error in Ruby text comparison?


Blogs related to Truth value evaluating of an object

Lockergnome Nexus
Rails is an integrated framework that utilizes the dynamic nature of the object-oriented Ruby programming language. Rails developers emphasize features of the framework for productivity rather than tools, and they take a common approach ...

Fine
... define: inuiversal truth define trivet? define inhouse blasting confined space rescue harness worksite health promotion evaluation how to read fine print glba violations and fines define mil-std 1553b databus coupler define: linda ...

24 new messages in 12 topics - digest
There's no other way to avoid unnecessarily evaluating the second operand besides using jumps. 2. POP_TOP [used even in normal test & jump situations] is horrible, and there are long-known better ways of doing it: ...

25 new messages in 14 topics - digest
The procedure followed here finds mpq(3,5) because evaluating "3.0/5.0" in as binary floating point numbers in double precision gives you the same thing as evaluating "0.6". So mpq(3,5) is the most parsimonious rational number ...

24 new messages in 8 topics - digest
and all will be well. The True object isn't the only truthy value in > > Python - see truth.html>. > > I would recommend the OP try this: > > run the (I)python shell and try the following: ...

25 new messages in 15 topics - digest
"call by value" by all means > I also think that I am not the only one to do so. You are not insinuating that the truth value of your statement somehow depends on the higher number of supporters, do you? :-) ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional