This scene does appear in the play (full text borrowable from the Internet Archive):
EARL OF GURNEY: Bring back fear. In the old days the Executioner kept the forelock-touching ranks in order. When he stood on the gallows, stripped to the waist, tight breeches, black good, you knew God was in his heaven, all's right with the world. The punishment for blaspheming was to be broken on the wheel. First the fibula. (Mimes bringing down an iron bar.) Cr-a-a-ck. Then the tibia, patella and femur. Crack, crack, crack. The corpus, ulna and radius, crack. 'Disconnect dem bones, dem dry bones. Disconnect dem bones, dem dry bones. Now hear the word of the Lord.'
Irresistibly the two women join in.
EARL OF GURNEY, MRS PIGGOT-JONES and MRS TREADWELL (singing): 'When your head bone's connected from your neck bone, your neck bone's connected from your shoulder bone, your shoulder bone's connected from your backbone. Now hear the word of the Lord. Dem bones, dem bones, dem dry bones. Now hear the word of the Lord . . .'
EARL OF GURNEY: We understand each other perfectly. But that's only to be expected. Breeding speaks to breeding.
As the play premiered on 6 November 1968 and the finale of The Prisoner aired on 1 February 1968, it's reasonable to suppose that this scene written by Peter Barnes was an homage to Patrick McGoohan's The Prisoner, a series which quickly achieved a cult following and has inspired references in countless other works of art, including even young-adult fantasy novels from the 2000s.
For what it's worth, both an IMDb review of the film adaptation of The Ruling Class and a 2014 blog post on Peter O'Toole mention this scene in the play/film as inspired by The Prisoner:
There are two great musical sequences, a hunt club performance of "Dem Bones" (a homage to "The Prisoner") and the climatic scene in the House of Lords (a surreal montage of decay to the music of "Pomp and Circumstances" and "Onward, Christian Soldiers").
It's always important to close out with a song, so I will conclude here with the raucous version of “Dem Bones” from The Ruling Class. One can't help but think that the film's scripter Peter Barnes dug up this old chestnut because of the last episode of The Prisoner; the song of course was also done to a fine turn in Potter's The Singing Detective.