Easy Question about P-names Notation(from Kunnen): $M$ is countable transitive model of $ZFC$, $P$ is a partial order that belongs to $M$, $G$ is a $P$-generic filter over $M$, $\hat{b}$ will be a $P$-name for $b\in M$, and $M[G]$ is the smallest model of $ZFC$ containing $G$ and $M$. 
The exercise i'm trying to do is the following: If $f$ is a function with range contained in $M$ and $f$ belongs to $M[G]$ then there is a set $B$ that belongs to $M$ containing the range of $f$. The hint given is to consider 
$B=\left \{ b:\exists p  \in P(p\Vdash \hat{b} \in \operatorname{Rng}(\tau ) ) \right \} $ where $\operatorname{val} (\tau,G) = f$
I understand that this describes the desired $B$ as a class in $M$ but i can't find a way to show that this is actually a set in $M$.
Thanks for the help and sorry for the trivial question.
 A: Here is a quick proof, which doesn't use the hint. 
If $f$ is a function in $M[G]$ with $\text{ran}(f)\subset M$, then since $\text{ran}(f)$ is a set in $M[G]$, it has some von Neumann rank $\alpha$ there. Thus, every element of $\text{ran}(f)$ has rank less than $\alpha$. Since rank is absolute and $\alpha$ is an ordinal in $M$, it follows that $\text{ran}(f)\subset (V_\alpha)^M$, and this is a set in $M$.
(This proof requires you already to know that $M$ and $M[G]$ have the same ordinals and that $M[G]$ satisfies ZFC.)
A: The class $B$ is the class of all elements that some condition forces them into $\rm{Rng}(f)$.
The question is, indeed, why is this a set in $M$.
Note that: $B_p = \{b\colon p\Vdash \hat b\in\operatorname{Rng}(\tau)\}$ is a definable class in $M$, since $p$ cannot force a proper class of elements into the range of $f$ we have that this is a set in $M$.
Suppose some $p$ would force $B_p$ to be a proper class of elements, then any generic filter concentrating on $p$ would force $f$ to be a proper class. Take $G$ to be a generic filter concentrating on $p$ and derive contradiction.
Now $\displaystyle B=\bigcup_{p\in P}B_p$ is also a set in $M$ by axioms of replacement and union.
Let's look at a concrete example. Consider a Cohen forcing that introduces one new real number, that is a new function $f\colon\omega\to 2$, or a new $A\subseteq\omega$.
Of course, for different $P$-generic filters, the resulting set is going to be different (that is, one filter might force $f(0)=1$ and another the opposite). However for every $p\in P$ we can define the set $B_p = \{n\colon p\Vdash n\in A\}$.
Note if we consider $P$ to be partial functions from some $n$ into $2$ (ordered by extension) then $B_p$ is exactly $\{n\colon n\in\operatorname{Dom}(p)\land p(n)=1\}$, i.e. all the numbers on which $p$ decides that are in the set.
Of course that in this rather trivial example $B = \omega$, and the $B_p$ was extremely simple to define, but I hope the idea got through.
