Commits

David Schneider committed 5d3d11f

extend if method to support any object as condition

Comments (0)

Files changed (3)

 
 @register_method('Object', 'if')
 def object_if(space, w_target, w_message, w_context):
-    w_condition = w_message.arguments[0].eval(space, w_context, w_context)
-
-    if w_condition is space.w_true:
+    w_condition = w_message.arguments[0].eval(space, w_target, w_context)
+    if space.istrue(w_condition):
         index = 1
     else:
         index = 2
 
     if index < len(w_message.arguments):
         return w_message.arguments[index].eval(space, w_context, w_context)
-    return w_condition
+    return space.newbool(index == 1)
 
 @register_method('Object', 'stopStatus')
 def object_stopstatus(space, w_target, w_message, w_context):
             return self.w_true
         return self.w_false
 
+    def istrue(self, value):
+        if value is not self.w_false and value is not self.w_nil:
+            return True
+        return False
+
     def newsequence(self, value):
         return self.w_immutable_sequence.clone_and_init(value)
+
     def isnil(self, w_object):
         return w_object is self.w_nil
 

io/test/test_object.py

     res, space = interpret(inp)
     assert res is space.w_false
 
+def test_if_object():
+    inp = 'if(Object)'
+    res, space = interpret(inp)
+    assert res is space.w_true
+
+    inp = 'if(nil)'
+    res, space = interpret(inp)
+    assert res is space.w_false
+
 def test_object_stopStatus():
     inp = 'stopStatus'
     res, space = interpret(inp)