The Story of Ruth is set in the time of Judges (Reference: Ruth 1-4).
Elimelech, a native of Bethlehem, migrated to Moab at a time of famine. He was accompanied by his wife, Naomi, and his two sons, both of whom married Moabite women.
Elimelech and his two sons died in Moab. Naomi decided to return to her own people in Bethlehem. Ruth, her Moabite daughter-in-law insisted on going with her.
On their return, the two women found themselves living in poverty; and Ruth, as a foreigner, had additional pressures to cope with (See Note).
In due course, Ruth met Boaz. Not only was Boaz a rich and generous farmer living in Bethlehem, he was a distant relative of her husband's family. Moved by her loyalty to Naomi, Boaz fulfilled his family duty and married Ruth, even though Ruth was not an Israelite.
Ultimately, Ruth became the great-grandmother of King David.
At the time that the story of Ruth was written, ethnic purity was being enforced among the returning exiles. Many existing mixed marriages between Jews and non-Jews were being deliberately broken up, and there was increasing hostility against people of all other races.