If $f : \mathbb{R} \rightarrow \mathbb{R}$ is a function, such that all distinct $x,y \in \mathbb{R}$ we have that:
$$\frac{f(x)-f(y)}{x-y} \not\in \mathbb{Q}$$
Can we conclude that $f$ is a linear function?
If $f : \mathbb{R} \rightarrow \mathbb{R}$ is a function, such that all distinct $x,y \in \mathbb{R}$ we have that:
$$\frac{f(x)-f(y)}{x-y} \not\in \mathbb{Q}$$
Can we conclude that $f$ is a linear function?
Assuming Axiom of Choice, here is a counterexample. Take a Hamel basis $\{b_\alpha \mid \alpha \in A \}$ of $\mathbb R$ over $\mathbb Q$, and let $A$ be well-ordered by a relation $\prec$. Let $J$ be an injective function from $A$ to itself such that $\alpha \prec J(\alpha)$ for all $\alpha$. We define $f$ such that if $x = \sum_{\alpha \in A} r_\alpha b_\alpha$ is the representation of $x$ in the Hamel basis, $f(x) = \sum_{\alpha \in A} r_\alpha b_{J(\alpha)}$. Note that $f(0) = 0$ and $f(b_\alpha) = b_{J(\alpha)}$. In order to ensure $f$ is not linear, we can ensure that $b_{J(\alpha_1)}/b_{\alpha_1} \ne b_{J(\alpha_2)}/b_{\alpha_2}$ for two particular members $\alpha_1$, $\alpha_2$ of $A$.
For any $x$ and $y$, if
$x - y = \sum_{\alpha} r_\alpha b_{\alpha}$, $f(x) - f(y) = \sum_{\alpha} r_\alpha b_{J(\alpha)}$. Suppose $x \ne y$. Let $\alpha_0$ be the least $\alpha$ (in the well-ordering) such that $r_\alpha \ne 0$. For any $s \in \mathbb Q$,
$$ f(x) - f(y) - s (x - y) = \sum_{\alpha} r_\alpha b_{J(\alpha)} - \sum_{\alpha} s r_\alpha b_\alpha$$
In the case $s = 0$, the coefficient of $b_{J(\alpha_0)}$ is $r_{\alpha_0} \ne 0$;
otherwise, the coefficient of $b_{\alpha_0}$ is $-s r_{\alpha_0} \ne 0$. We conclude $(f(x) - f(y))/(x-y) \notin \mathbb Q$.
As I mentioned in comments, if $f$ is assumed continuous, then the result is true. Indeed, the function $(x, y) \mapsto \frac{f(x) - f(y)}{x - y}$ is continuous on $\{(x, y) \in \mathbb{R}^2: x > y\}$, which is connected. So by the intermediate value theorem, the condition implies there exists $k \in \mathbb{R} \setminus \mathbb{Q}$ s.t. $\frac{f(x) - f(y)}{x - y} = k$ whenever $x > y$ (as otherwise the range is a nontrivial interval, which must contain a rational number), from which it is clear that $f$ is linear.
Now, here is a counterexample without that assumption. Let $\mathfrak{c}$ be the cardinality of continuum. Let $\{r_\kappa\}_{\kappa < \mathfrak{c}}$ be a $\mathbb{Q}$-basis of $\mathbb{R}$. We shall choose, by transfinite induction, a sequence $\{k_\kappa\}_{\kappa < \mathfrak{c}}$ of irrational numbers, s.t., at each $\kappa < \mathfrak{c}$, we have,
$$\frac{\sum_{\alpha \leq \kappa} q_\alpha k_\alpha r_\alpha}{r} \notin \mathbb{Q}$$
Assume for some $\lambda < \mathfrak{c}$, $k_\alpha$ for all $\alpha < \lambda$ have been chosen and the above condition is satisfied when $\kappa < \lambda$. We shall choose $k_\lambda$ s.t. the above is also satisfied when $\kappa = \lambda$. Note that the first condition only excludes $|\lambda|$ many choices, namely $k_\alpha$ when $\alpha < \lambda$. The second condition only excludes at most $\max\{|\lambda|, \aleph_0\}$ many choices, since, if $r = \sum_{\alpha \leq \lambda} q_\alpha r_\alpha$ is s.t. $q_\lambda = 0$, then the condition is already satisfied by the inductive assumption. Otherwise, there are only $\max\{|\lambda|, \aleph_0\}$ many choices of $r$, and for each such $r$ and each $q \in \mathbb{Q}$, only one possible choice of $k_\lambda$ that would cause $\frac{\sum_{\alpha \leq \lambda} q_\alpha k_\alpha r_\alpha}{r} = q$. Thus, there are at most $\max\{|\lambda|, \aleph_0\} \times \aleph_0 = \max\{|\lambda|, \aleph_0\}$ many possible choices of $k_\lambda$ that are excluded by the second condition. Thus, in general, there are at most $\max\{|\lambda|, \aleph_0\} + |\lambda| = \max\{|\lambda|, \aleph_0\} < \mathfrak{c}$ many possible choices of $k_\lambda$ that would fail the requisite conditions. Since there are $\mathfrak{c}$ many irrational numbers, we may thus choose a $k_\lambda$, as required.
Now, we may define $f$ by,
$$f(\sum_{\alpha < \mathfrak{c}} q_\alpha r_\alpha) = \sum_{\alpha < \mathfrak{c}} q_\alpha k_\alpha r_\alpha$$
Where $q_\alpha$ are all rational and all but finitely many $q_\alpha$ equal $0$. Recall that $\{r_\kappa\}_{\kappa < \mathfrak{c}}$ is a $\mathbb{Q}$-basis of $\mathbb{R}$, so $f$ is a well-defined $\mathbb{Q}$-linear map. It is not ($\mathbb{R}$-)linear, since, say $\frac{f(r_0) - f(0)}{r_0 - 0} = k_0 \neq k_1 = \frac{f(r_1) - f(0)}{r_1 - 0}$. But by the inductive construction, we see that $\frac{f(r)}{r} \notin \mathbb{Q}$ for all $r \in \mathbb{R} \setminus \{0\}$, so by $\mathbb{Q}$-linearity, whenever $x \neq y$,
$$\frac{f(x) - f(y)}{x - y} = \frac{f(x - y)}{x - y} \notin \mathbb{Q}$$