4thSpace 4thSpace - 1 year ago 71
Ruby Question

Why isn't this truthy?

gets assigned a value. That should then make any call to
on this instance truthy. Instead,
returns nil. What am I doing wrong?

class ClassA
attr_accessor :prop

def test_it?


a = ClassA.new
a.prop = "test"
if puts a.test_it?

Answer Source

if you want the if statement to evaluate to true, you need to do

if a.test_it?
  # code to execute unless statement is false or nil

The if statement will evaluate to true if @prop is not false or nil. So if you assign @prop to be test, then the if statement is true, and the code inside the if is evaluated.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download