All core functionality is in place, so time for more shapes. First up is cubes, the volume contained by three perpendicular pairs of parallel planes, and aligned with the axes. So planar ray intersection with each one, and pick the snuggest fit to intersect the cube. Not quite pixel ready yet, so something for tomorrow.

The tricky bit is that when any component of the ray direction is zero, intersections with some of the planes causes the intersection equation \(o + \overrightarrow{d}t = 0\) to divide by zero, and for near misses even zero divide by zero. Since \(0/0 \rightarrow NaN\) that is detected and replaced by \(+Inf\). The other divisions by zero gives the correct \(±Inf\) for Ruby Floats, so that’s fine out of the box.

def nonan_div(numerator, denominator)
  return numerator / denominator unless numerator.zero? && denominator.zero?

  Float::INFINITY
end