I saw this question a while ago. I wasn’t able to answer it before hearing the following solution, which isn’t mine.
For \(x\) in \(\mathbb{R}\), take its binary representation* \(b_1b_2\ldots b_n.b_{n+1}b_{n+2}\ldots\) and form
\[ H_x = \sum_{i=1}^\infty \frac{(-1)^{b_i}}{i}\text{.} \]Then let
\[ f(x) = \begin{cases} H_x & H_x \text{ converges}\\ 0 & H_x \text{ diverges.} \end{cases} \]Since any tail of the harmonic series can be made to converge to any real number by changing the signs of terms in the appropriate way, and in every non-empty open interval \((a,b)\) all infinite binary strings on \(\{0,1\}\) are represented* from some sufficiently late binary place, we have \(f((a,b))=\mathbb{R}\).
*Some numbers have two representations, in which case we ignore the one that ends with an infinite number of \(1\)s. These are therefore not represented anywhere, but their sign application to the harmonic series would always diverge anyway, so no numbers are missed.