The chemistry involved in producing this gas is well known. The compounds responsible are a subset of the carbohydrate family, termed oligosaccharides. These hover between the longer-chain starches and the mono- and disaccharides that are digested by the yeast when we ferment our wort into beer. Given what they do, they would be classified by brewers as "unfermentables."
As is the subject of endless humor, going back to the literature of ancient times ("wind eggs" and whatnot), in the process of digestion some of these oligosaccharides, present in a variety of foods, produce methane in the process of being broken down. Some folks produce enzymes that can handle this digestion without nearly as much gas, others not so much, and it doesn't improve with age......as I can personally testify.
This is a situation analogous to the inability of some groups of humans (notably SW Asia) to digest raw milk without significant digestive upset. Thus, the wide development and use of yogurt in this part of the world, in which the milk is transformed by bacteria into a more digestible product.
The only answers I know of are to either avoid such foods (I don't know about you, but when it comes to beer, forget about it!), or resort to a commercial product like Beano, which simply attempts to replace an enzyme that will convert the oligosaccharides into something less likely to produce a re-enactment of the Hindenburg disaster.