Learn a little about AABB to Ray intersect instead of just copying the code. It's not that difficult if you know vector math. But there are other guides to help you.
Another topic about AABB to Line Segment ( has Ray to AABB code )
Video Explaining Ray to AABB ( No code, just the math side )
A little guide to creating Ray to AABB Checks ( Has code, gives a little bit of explanation for it too )
After you complete that, I would look into using OBB's instead of AABB's if your cube is going to be changing orientation.