How do inanimate objects eat devil fruit? Devil fruit users gained their powers by eating the devil fruit. But how did these inanimate objects gain their power? 

This is Luffy, eating a devil fruit before he became a rubber man.



And this is Lassoo, one of Mr.4's guns that became a dog. How did a gun ate it?
As mentioned in the comment, Dr. Vegapunk's scientific expertise is the logic behind an inanimate object being able to "consume" a devil fruit. 


  Dr. Vegapunk covered exactly how Devil Fruit powers are transferred over to a user, and was the scientist behind the method of adding Devil Fruit powers to inanimate objects, such as swords and guns.Source


This is the only known research paper of his related to this experiment:



The inanimate objects who have eaten Devil Fruits are:


Mr. 4's  gun, Lassoo 
Spandam's sword, Funkfreed.


It is also notable that the only Devil Fruits shown to be capable of achieving such a feat are Zoan devil fruits. It may be because Logia and Paramecia only grant the users supernatural powers, which would not bring the objects to life. 

This is all that is known about the matter.


  According to Oda, a "certain professor" will explain the complex mechanisms behind all of the Devil Fruits in the series in the near future. Source