You have not said all that you know or guess, Aragorn my friend,' he said quietly. 'Poor Boromir! I could not see what happened to him. It was a sore trial for such a man: a warrior, and a lord of men. Galadriel told me that he was in peril. But he escaped in the end. It was not in vain that the young hobbits came with us, if only for Boromir's sake. - Gandalf the White speaking to Aragorn
In the Two Towers Gandalf tells Aragorn that Boromir escaped from peril and that it may be that Merry and Pippin were chosen as part of the company for exactly that reason. What peril did Boromir escape from? True, he attempted to steal the Ring from Frodo, and the Ring is obviously a peril for anyone but Sauron to possess. But Frodo escaped and Boromir came to his senses. It seems that once Frodo was gone then Boromir was free from the peril of the Ring. If that's the case then why would Gandalf be happy that the inclusion of Merry and Pippin in the company was a factor that lead to Boromir's death after the Ring was no longer within his reach?