Bart's class is interrupted by “Robbie the Automaton,” a robot created by the Knowledgeum, a new museum that offers a hands-on approach to learning about science. The family go there the following Saturday, where Bart wanders off and walks into an exhibit of the planet Mars. He meets Ralph, who is in the process of being pushed into a giant ear by Kearney, Jimbo, Nelson and Dolph. When Ralph is freed by a center employee, Marge and Chief Wiggum are there to meet him. Marge observes that Ralph has a vivid imagination and learns that he has no friends to play with; she arranges a play-date for Ralph to spend time with a horrified Bart. During the play-date, Ralph shows Bart his swing set, his sandbox, and a rock where he met a leprechaun, who tells him to burn things.
Bart, who does not want to be seen in public with Ralph, has a scare when the two encounter Nelson's gang of bullies riding a stolen vehicle with a video (Bart covers this up by hastily convincing Ralph to hide in a prickly bush), but later exploits it when he discovers Ralph's father has a police master key capable of opening any door in Springfield. Bart and Ralph thus steal the key and decide to enter several closed stores at night, such as a toy store and a bakery, having fun doing whatever the two can imagine. After encountering Nelson and his gang, the boys go to an unused penitentiary. When Ralph objects because he is afraid, the bullies throw him and Bart into a muddy puddle and leave to pick huckleberries — but not before tossing the key into the penitentiary. Ralph and Bart enter the prison to retrieve the key, and in the process stumble onto a room housing an old electric chair. Eager with curiosity to test it they melt the couple from atop a wedding cake they had gorged themselves on earlier that night. The two flee when an elderly guard approaches, but accidentally leave the electric chair armed.
Afterwards at the Simpsons home, Bart and Ralph learn the penitentiary will once again be used by the town, and remember that they left the power on; Bart tries to call the prison and warn them, but the elderly guard has tied up the line having a completely pointless conversation with his wife. Unaware that the power is active, Mayor Quimby straps himself into the electric chair in a publicity attempt, and his staff follows his command to not turn off the chair no matter how realistic his "execution" looks. Ralph then tells Bart that Lisa can probably figure out a way to warn the Mayor. She decides to launch a small model rocket with a warning message attached and aims it toward the penitentiary. However, the rocket is blown off-course and crashes through Mr. Burns' office window. Mr. Burns reads the note, and realizes that the penitentiary has been receiving free power for over thirty years; he responds to it by shutting off the penitentiary's power. Mayor Quimby does not feel very well but escapes being killed. In the aftermath, the Simpsons congratulate Ralph, for pointing out that Lisa could solve the problem. Lisa is at first baffled that she received no credit for having thought of the solution and implementation, but she is reminded by Bart that it is rare for Ralph to be useful or praised, and that he needs a little credit. Lisa then joins in the praising. Ralph's imaginary leprechaun friend congratulates him, then tells him to burn the Simpsons' house down.