Evidence indicates that Ruby's action was likely a spur-of-the-moment decision. He had a history of violent behavior; further, he left his beloved dogs in his car when he entered the basement; had he premeditated his action, he likely would have left them at home. See
here.
I have just been rereading the Ruby materials from the Warren Commission and elsewhere (after a friend said Ruby's motive was the one thing that didn't make sense to him). It does seem that it was spur of the moment: the Oswald transfer was an over an hour late, Ruby was only in the area to wire money through Western Union (less than a block from the garage), and Ruby always carried a gun when he had large sums of cash (he was carrying $2,000 at the time).
Background: Many witnesses testified that he was both sentimental and had a tendency to violence.
Motive: A few. First, he felt like he was part of history. He said so explicitly in his WC testimony. Witnesses say he told them this before the shooting, as well. He was at the press conference where Oswald spoke, and apparently really felt he was part of it, even correcting a statement made. He spent much of the 23rd investigating who placed an "Impeach Earl Warren" ad and who printed a Kennedy wanted poster, thinking they were involved. He went all over town following leads about this. He also bought sandwiches for cops who were working overtime, and he set up interviews with the DA for a radio station. In all of this, you get the sense of someone who increasingly feels important and closely involved.
Second, the assassination bothered him and he wanted to do something. Everyone who saw him that day remarked on how broken up he was. He closed his clubs for three days. Several witnesses testify he called Oswald a zero or a weasel, and he was upset that he could kill the president. Police testify that after arrest Ruby said someone had to kill Oswald, but the police couldn't.
Third, he wanted to spare Jackie having to testify at a trial. Several witnesses heard him lament her plight before he killed Oswald. After, this was the motive he proclaimed most often, probably because it is more admirable than his other motives.
He also said other things relating to motive, but the above is enough to suggest to me what allowed the impulse to kill Oswald to overtake him in that garage.