This would work

    module A
      module B
        class B2
          class B1Error < Exception ;end
          class B2_inner
          end
        end
      end
    end

    def nested_classes(root)
      [].tap do |klasses|
        root.constants(false).map do |name|
          if (value = root.const_get(name)).is_a?(Module)
            klasses << value if value.is_a?(Class)
            klasses.concat(nested_classes(value))
          end
        end
      end
    end

    p nested_classes(A)
    # => [A::B::B2, A::B::B2::B1Error, A::B::B2::B2_inner]

Note however that the nesting does not necessarily reflect nested
namespaces.