Cesar Hernandez tripled and doubled, Maikel Franco singled twice, and Juan Soto had three walks for the Nationals, who avoided their second three-game sweep to the Marlins this season.
Ruiz's smash down the third-base line off reliever Daniel Castano (0-1) scored automatic runner Dee Strange-Gordon from third. For Ruiz, the hit capped a performance that also included another double, a single and two walks.
''I wasn't trying to do too much, just look for my pitch,'' Ruiz said. ''I found something that I could drive and bring in the run. Thank God I found that pitch and we got the result we wanted.''
Jazz Chisholm Jr.'s sacrifice fly against Washington reliever Tanner Rainey (1-1) with the bases loaded in the ninth tied it at 4. The Nationals unsuccessfully appealed that Jesus Sanchez left third base before Soto caught Chisholm's shot in right.
Victor Arano worked the 10th for his first save.
The Nationals broke a 3-3 tie after Miami reliever Anthony Bender hit Lane Thomas with the bases loaded in the eighth.
Marlins starter Pablo Lopez, who began the day with a major league-best 1.05 ERA, had his shortest outing of the season, working three innings on 82 pitches. Lopez allowed three runs, four hits, walked two and struck out two. The right-hander's ERA rose to 1.57.
''Today was the least comfortable I've felt on the mound so far and it was an odd feeling so I was trying to recreate some stuff instead of trying to simplify,'' Lopez said. ''I was trying to find something that wasn't there.''
Chisholm, who didn't feel well during the game, tied it at 3 with his solo shot against starter Josiah Gray to lead off the fifth. Chisholm drove Gray's fastball over the wall in center for his seventh homer.
''I'm still feeling pretty bad right now, got the shivers all day and still feeling them now,'' Chisholm said. ''Earlier today, I was just resting all day and when I got to the field, they asked me if I could still play. I felt like I could just play through it, like I could flip the switch when I needed to do it.''
Gray was lifted after six innings. He allowed three runs, six hits and struck out seven and hit a batter.
''You just go out there and attack the zone and put up zeroes for the team,'' Gray said. ''More times than not, if I get it to the spot I want to, it's going to be a good pitch.''
Consecutive RBI doubles from Alcides Escobar and Hernandez in the second put Washington ahead 3-1 before the Marlins narrowed the deficit on Garrett Cooper's sacrifice fly in the third.
The Nationals struck quickly against Lopez. Hernandez hit a leadoff triple and scored on a wild pitch.
''We had a game plan against Pablo, he's a tremendous pitcher,'' Nationals manager Dave Martinez said. ''We wanted to see him throw the ball up and stay away from his changeup. We did a good job with that.''
Miami responded in the bottom half as leadoff hitter Chisholm also tripled and raced home on Jesus Aguilar's sacrifice fly.
Hernandez set the tone early with his nine-pitch at bat in the first. The Nationals made Lopez quickly build up his pitch count, resulting in his brief outing. After Hernandez, the four other batters in the inning drew at least five pitches.
''That was awesome,'' Martinez said of Hernandez's at bat. ''He doesn't get enough credit because he's not your typical leadoff hitter but the guy swings the bat. He's swinging the bat well.''
Marlins: OF Jorge Soler (back stiffness) missed his second consecutive game.
Nationals: RHP Erick Fedde (2-2, 4.24) is the scheduled starter in the opener of a three-game set at Milwaukee Friday.
Marlins: LHP Trevor Rogers (2-4, 4.45) will start the opener of a home series against Atlanta Friday.
More AP MLB: https://apnews.com/hub/MLB and https://twitter.com/AP-Sports
Copyright 2023 STATS LLC and Associated Press. Any commercial use or distribution without the express written consent of STATS LLC and Associated Press is strictly prohibited.