In
The Hobbit, chapter VIII, "Flies and
Spiders",
Bilbo is forced to use
the Ring's power of invisibility to rescure the Darves frmo the spiders, following which:
They had to have the whole vanishing business carefully explained, and the finding of the ring interested them so much that for a while they forgot their own troubles.
Balin in particular insisted on having the
Gollum story,
riddles and all, told all over again, with the ring in its proper place.
Word about it must have spread somewhat, for in "The Return Journey" Bilbo has a convesation with the Elf-king which makes no sense if the Elf-king did not know a great deal about Bilbo's part in freeing the
Dwarves and that Bilbo had been living invisibly in his
halls. It ends with:
"I will take our gift, O Bilbo the Magnificent!" said the king gravely. "And I name you elf-friend and blessed. May your shadow never grow less (or stealing would be too easy)! Farewell!"
We have been told earlier that though the Ring makes its wearer invisible, the wearer's shadow can still be seen in bright light, which is what the Elf-king refers to.
In light of this, it would be more surprising to later discover that
Gandalf doesn't know about the Ring than to find that he knows about it.
The Foreward to
The Fellowship of the Ring simply confirms what one would normally expect, that Gandalf did learn about the Ring (whether first from the Dwarves or from Bilbo himself). It states:
Now it is a curious fact that this is not the story as Bilbo first told it to his companions. To them his account was that Gollum had promised to give him a
present, if he won the game; but when Gollum went to fetch it from his island he found the treasure was gone: a
magic ring, which had been given to him long ago on his birthday.
<snip>
Gandalf, however, disbelieved Bilbo's first story, as soon as he heard it, and he continued to be very curious about the ring. Eventually he got the true tale out of Bilbo after much questioning, which for a while strained their friendship; but the wizard seemed to think the truth important.
There is no contradition on this point between
The Hobbit and
The Lord of the Rings at all.
The only difference is that
The Hobbit does not indicate that Bilbo's first story of how he obtained the ring was not altogether truthful.
Some may be confusing this first story of how Bilbo obtained the Ring with Bilbo's earlier account to the Dwarves and Gandalf of his encounter with Gollum which did not mention the Ring at all.
This idea of Bilbo lying originally arose because
Tolkien modified the account of the
riddle game which appeared in the first edition of
The Hobbit, so that Gollum did not actually give up the Ring, but rather than subsequently ignore that first version, now to be considered incorrect, he explained it as the version that Bilbo actually put down in the Red Book, though the alternate and more accurate account also purportedly appeared in some copies of the Red Book.