Truth value evaluating of an object

B

Byron

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top