Cesar Hernandez started the winning rally with a two-out walk against Tyler Clippard (6-3). He came around to score on Jimmy Rollins' double. After Chase Utley was walked intentionally, Ruiz drove in Rollins with a single to left.
B.J. Rosenberg (1-0) recorded the final out of the eighth for the win and Jonathan Papelbon finished for his 24th save in 30 chances.
The Nationals put runners on the corners with one out in the ninth, but Wilson Ramos was called out on strikes and Anthony Rendon struck out swinging to end the game.
Ryan Zimmerman homered for Washington, which had won nine of 12. The Nationals fell 7½ games back of Cincinnati for the second NL wild card.
The Nationals took a 2-1 lead in the eighth on Scott Hairston's bases-loaded sacrifice fly that scored Anthony Rendon. Hernandez, a converted infielder who was playing his first career game in center, fielded the ball in shallow center field but his throw went weakly up the third-base line.
Philadelphia left-hander Cole Hamels and Washington right-hander Stephen Strasburg each pitched well before turning a tie game over to the bullpens.
Hamels struck out eight in seven sharp innings, limiting the Nationals to one run and two hits. He threw 87 pitches, 69 for strikes.
Hamels has a 1.90 ERA in his last seven starts but has just two wins during that span. The left-hander's record stands at 6-13, thanks partly to the fourth-lowest run support (3.44) among all major league starters entering Monday's action.
Strasburg fanned 10 while giving up an unearned run and two hits in six innings. He threw 100 pitches, 64 for strikes.
Zimmerman connected on an 0-2 pitch in the first for his 16th homer. Washington got just two baserunners off Hamels from that point, with Ramos reaching on an error in the fifth and Bryce Harper singling in the sixth.