I apologize in advance for the length of the question.
The book of Ruth is incredibly romantic and powerful, but I don’t understand the legal portion of the drama:
Now Boaz had gone up to the gate and sat down there. And behold, the redeemer, of whom Boaz had spoken, came by. So Boaz said, “Turn aside, friend; sit down here.” And he turned aside and sat down. And he took ten men of the elders of the city and said, “Sit down here.” So they sat down. Then he said to the redeemer, “Naomi, who has come back from the country of Moab, is selling the parcel of land that belonged to our relative Elimelech. So I thought I would tell you of it and say, ‘Buy it in the presence of those sitting here and in the presence of the elders of my people.’ If you will redeem it, redeem it. But if you will not, tell me, that I may know, for there is no one besides you to redeem it, and I come after you.” And he said, “I will redeem it.”—Ruth 4:1-4 (ESV)
So far, so good. The property must remain in the family:
If your brother becomes poor and sells part of his property, then his nearest redeemer shall come and redeem what his brother has sold.—Leviticus 25:25 (ESV)
(In passing, it’s interesting to see that Naomi would be paid for the land so the effect of the rule is that widows retained some form of property ownership.)
Then Boaz said, “The day you buy the field from the hand of Naomi, you also acquire Ruth the Moabite, the widow of the dead, in order to perpetuate the name of the dead in his inheritance.” Then the redeemer said, “I cannot redeem it for myself, lest I impair my own inheritance. Take my right of redemption yourself, for I cannot redeem it.”
Now this was the custom in former times in Israel concerning redeeming and exchanging: to confirm a transaction, the one drew off his sandal and gave it to the other, and this was the manner of attesting in Israel.—Ruth 4:5-7 (ESV)
I do see that the custom of perpetuating the name of the dead had legal basis:
“If brothers dwell together, and one of them dies and has no son, the wife of the dead man shall not be married outside the family to a stranger. Her husband’s brother shall go in to her and take her as his wife and perform the duty of a husband’s brother to her. And the first son whom she bears shall succeed to the name of his dead brother, that his name may not be blotted out of Israel. And if the man does not wish to take his brother’s wife, then his brother’s wife shall go up to the gate to the elders and say, ‘My husband’s brother refuses to perpetuate his brother’s name in Israel; he will not perform the duty of a husband’s brother to me.’ Then the elders of his city shall call him and speak to him, and if he persists, saying, ‘I do not wish to take her,’ then his brother’s wife shall go up to him in the presence of the elders and pull his sandal off his foot and spit in his face. And she shall answer and say, ‘So shall it be done to the man who does not build up his brother’s house.’ And the name of his house shall be called in Israel, ‘The house of him who had his sandal pulled off.’—Deuteronomy 25:5-10 (ESV)
But why does Boaz say, “The day you buy the field from the hand of Naomi, you also acquire Ruth the Moabite”? How does redeeming some land also introduce a levirate marriage obligation?