Another speculation that roger had the gomu gomu no mis previous owner was Roger(although i dont believe it but this thought seemed convincing). So the theory is Rayleigh was the closest person on the PKs ship so he knew a lot about him. If Roger had the fruit, he would also discover(if i may?) the gears(cuz even an idiot like Luffy discovered em). Rayleigh would also know about them cuz he was his first mate. So using this knowledge of gears Rayleigh taught the fourth and some other transformations to Luffy.