Let $\mathbb{F}$ be an algebraically closed field. We call $\mathbb{F}^n$, $\mathbb{A}^n$, affine $n$-space. For an affine algebraic set $X \subset \mathbb{A}^n$ and an open set $U \subset X$ we say $f:U \to \mathbb{F}$ is regular if, for each $x \in U$ there exists $g,h \in \mathbb{F}[T_1,\ldots,T_n]$ such that $h(x) \neq 0$ and $f = \frac{g}{h}$ in some open neighborhood of $x$. The set of all regular functions on $U$ is denoted $\mathcal{O}_X(U)$.
I want to show that $\mathcal{O}_X(X) = \mathbb{F}[X]$. That is, if for every $x \in X$, $f$ can be represented in a neighborhood of $x$ as $\frac{g}{h}$ with $h(x) \neq 0$, then $f$ can be represented globally by a polynomial.
I can prove it in the case that $X$ is an irreducible variety as follows:
The case when $X$ is a single point is clear. Assume $X$ is infinite. For each $x \in X$ we have $f = \frac{g_x}{h_x}$ on some neighborhood $U_x$ of $X$. As $X$ is irreducible, for any $U_x$ and $U_y$ we have $U_x \cap U_y \neq \emptyset$ and so on $U_x \cap U_y$, $\frac{g_x}{h_x} = \frac{g_y}{h_y}$ as functions. Since $U_x \cap U_y \subset X$ is dense, $g_xh_y = h_xg_y$ on $X$ and so we have $\frac{g_x}{h_x} = \frac{g_y}{h_y}$ in the ring of rational functions $\mathbb{F}(X)$. For any $x \in X$ we can write $f(y) = \frac{g_x(y)}{h_x(y)}$ for any $y$ with $h_x(y) \neq 0$. In particular, $f$ is a rational function and for each $x$ we can represent $f$ as $\frac{g_x}{h_x}$ where $h_x(x) \neq 0$. Then the zero set in $X$ of the ideal generated by all the functions $h_x$ is empty, so by the Nullstellensatz this ideal is $\mathbb{F}[X]$. Hence there exist functions $s_1,\ldots,s_n \in \mathbb{F}[X]$ and points $x_1,\ldots,x_n \in X$ such that $1 = \sum_{i = 1}^{n}s_ih_{x_i}$. Multiplying (in $\mathbb{F}(X)$) by $f$ and using the fact that $f = \frac{g_{x_i}}{h_{x_i}}$, we get $f = \sum_{i = 1}^{n}s_ih_{x_i}$ and so $f \in \mathbb{F}[X]$.
Now I'm struggling to extend this to the case when $X$ is not irreducible. I can break $X$ into irreducible components $X = X_1 \cup \cdots X_n$ and then on each $X_i$, $f|_{X_i}$ can be represented by a polynomial. However we've seen an example that tells us this alone is not sufficient to conclude $f$ is globally representable by polynomial.
Another route I've tried to take, for each $x \in X$ an open set $U_x$ so that $f = \frac{g_x}{h_x}$ on $U_x$ with $U_x \neq 0$. Then $\langle h_x : x \in X \rangle$ is not contained in any maximal ideal (by Nullstellensatz they all have the form $M_x = \{ g : g(x) = 0\}$) and so $\langle h_x : x \in X \rangle = \mathbb{F}[T_1,\ldots,T_n]$. In particular I can find $x_1,\ldots,x_k$ and $s_1,\ldots,s_k \in \mathbb{F}[T_1,\ldots,T_n]$ such that $1 = \sum s_i h_{x_i}$. Then $f = \sum s_i fh_{x_i}$ as functions on $X$. If I know that my representations of $f$ as $\frac{g_x}{h_x}$ were global then I would be finished from here, but I don't think I can quite get that.
A hint would be preferred but a full solution would also be fine.
Edit: Much cleaner proof (more recent) just below, original proof after.
We know that there is a finite cover of $X$ with open $U_i$ such that $f_{|U_i}=g_i/h_i$ where $g_i$ and $h_i$ are polynomials with $h_i$ not vanishing on $U_i$.
Define, for each $i$, $J_i$ to be the ideal of polynomials vanishing on $X \backslash U_i$. The elements of the $J_ih_i$ have no common zero in $X$ so (Nullstellensatz) there are $s_i \in J_i$ with $\sum_i{s_ih_i}=1$ on $X$.
Then, on $U_k$, $s_if=s_ig_i/h_i$ ($s_i$ vanishes outside $U_i$), so $s_ih_if=s_ig_i$. So the identity actually holds on $X$.
As a consequence, summing over $i$, we get $f=\sum_i{s_ig_i}$ is a polynomial.
(So you can stop reading here)
Original proof:
We know that there is a finite cover of $X$ with open $U_i$ such that $f_{|U_i}=g_i/h_i$ where $g_i$ and $h_i$ are polynomials with $h_i$ not vanishing on $U_i$.
Note that on $U_i \cap U_j$, we must have $g_ih_j=g_jh_i$. Let $J_{ij}$ is the ideal of polynomial functions vanishing on the complement in $X$ of $U_i \cap U_j$, thus $J_{ij}$ is the annihilator of $g_ih_j-g_jh_i$ as a polynomial function on $X$.
Consider the set of all pairs $i,j$ with $i \neq j$ and $U_i \cap U_j \neq \emptyset$, then consider the corresponding ideals $J’_{ij}=J_{ij}h_ih_j$. Since the elements of $J’_{ij}$ have no common zero in $X$, there are elements $s_{ij}$ of $J_{ij}$ such that $\sum_{i,j}{s_{i,j}h_ih_j}=1$ on $X$.
Note that if $i,k$ is a pair as above, as functions over $U_k$, $s_{ik}h_ih_kf=s_{ik}h_ig_k$ is a polynomial.
If $i,j$ is a pair above such that $k$ is neither $i$ nor $j$, note that as functions over $U_k$, $s_{ij}f=s_{ij}\frac{g_j}{h_j}$, since $s_{ij}$ vanishes outside $U_j$. Therefore, $s_{ij}h_ih_jf=s_{ij}h_ig_j$.
So, whatever the pair $i,j$, on each $U_k$, $s_{ij}h_ih_jf=s_{ij}h_ig_j$. So the identity holds on $X$.
Summing over the pairs $i,j$, we conclude that $f$ is the restriction of a polynomial.