4thSpace 4thSpace - 3 months ago 10
Ruby Question

Why isn't this truthy?

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

class ClassA
attr_accessor :prop

def test_it?
@prop
end

end

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

Answer

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
end

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.

Comments