This worked for me, albeit with a strange quirk: if I have a tilemap, and the collision ray collides with the tilemap, the while-loop ends up looping forever. It seems that - despite adding the tilemap as an exception to the collider - it still collides.
I don't know if this is a Godot bug or something specific about my setup; in the meantime, you can work around this, by simply adding something like this in the while loop:
if obj in objects_collide: break