\def\rubyfont{\rm}

\newtoks\rubybase
\newtoks\rubytext

\newdimen\rubywidth

\def\startRUBY
  {\bgroup}

\def\stopRUBY
  {\setbox0\hbox{\rubyfont\strut\the\rubytext\unskip}%
   \setbox1\hbox{\strut\the\rubybase\unskip}%
   \ifdim\wd0>\wd1
     \rubywidth\wd0
   \else
     \rubywidth\wd1
   \fi
   \vbox
     {\offinterlineskip
      \hbox to\rubywidth{\hss\box0\hss}%
      \hbox to\rubywidth{\hss\box1\hss}}%
   \egroup}

\def\startRB#1\stopRB{\rubybase{#1}}

\def\startRT#1\stopRT{\rubytext{#1}}

\def\ruby#1#2%
  {\startRUBY
     \startRB#1\stopRB
     \startRT#2\stopRT
   \stopRUBY}

\endinput
