Socrates has his own answer to this, which I won't get into here. It's interesting, however, to read St. Ambrose's response to the Ring of Gyges, which he addresses in his work De Officiis, usually called in English, "On the Duties of the Clergy." Very few people seem to know anything about it, but it's a classic of virtue ethics.*
Ambrose dismisses the story as a made-up tale; and the reason he does so is that he thinks we can learn more from real-life cases.** And he argues, using the examples of David protecting King Saul despite the fact that the latter was attempting to kill him and of John the Baptist facing down Herod, that there are clear real-life cases of people who do not succomb to the temptation of Gyges, but instead face even danger and death rather than do something unjust. We don't need the "pretense of a ring" to see that a wise man will rather face punishment than than do wrong. He then continues:
But although that fable has not the force of truth, yet it has this much to go upon, that if an upright man could hide himself, yet he would avoid sin just as though he could not conceal himself; and that he would not hide his person by putting on a ring, but his life by putting on Christ.
And he concludes that we should therefore not let expediency get the better of virtue, but, like the upright man, regard virtue as superior to expediency.
* Lest I be misunderstood, I should clarify this. The De Officiis is not a treatise on virtues but on (as you might guess) offices. The distinction is an important one in ancient virtue ethics that is almost entirely lacking in contemporary virtue ethics. Ambrose's De Officiis is effectively a Christianized version of Cicero's De Officiis. Cicero is presupposing a Stoic distinction between virtue, which in the Stoic view is the only good, and duty or office, which are derived from virtues but not virtues themselves. Offices, unlike virtues, presuppose well-defined social roles; while virtue relates directly to our final end, offices are primarily directed to ordering our everyday life and only indirectly to our final end; virtues are universal, offices can vary according to circumstances (although different offices differ in how much they vary). The distinction played and important role in early modern moral theory: the virtue theories of both Hutcheson and Hume are very Ciceronian, but Hume treats Cicero's De Officiis as a treatise on virtues. Hutcheson's view is that this Humean conflation is one of the fatal flaws in Hume's moral philosophy. Today's 'virtue ethicists' are Humean in this regard. Ambrose, however, is a solid Ciceronian: his book on offices is based on an account of virtue, but he doesn't conflate offices with virtues. They are a subordinate, secondary, derivative, field of inquiry.
** St. Ambrose's version differs slightly from Plato's version; in his account of the story, the message of the story is that "The hiding-place of the wise lies not in the hope of impunity but in his own innocency". This is indeed the message one eventually takes away, after Socrates is done with it; but it's not the immediate point of the tale, which is to present an argument for exactly the opposite conclusion. The reason is that Ambrose is not getting the story directly from Plato but from Cicero's De Officiis, and this (very Stoic) reading is Cicero's reading -- even the remark just quoted is found in Cicero. It's actually rather interesting, because Ambrose does not slavishly follow Cicero: Cicero immediately goes on to defend Plato against the charge that the story is false and fabulous by insisting that it is a hypothetical, while Ambrose dismisses it for precisely the same reason.