A simple trick to auto-install missing gems when requiring:
alias old_require require
def load_gem(gem)
old_require gem
rescue LoadError => e
print "'#{gem}' can't be found, trying to install it..."
system("gem install #{gem} --no-ri --no-rdoc")
Gem.clear_paths() && require(gem) && puts("loading '#{gem}' ok!")
end
## Usage
ruby
> require 'xkcd'
'xkcd' can't be found, trying to install it..
Fetching: xkcd-1.0.1.gem (100%)
Successfully installed xkcd-1.0.1
2 gems installed
=> nil
Note: Use Bundler for proper dependency management. This is just a fun hack!
#ruby
About Hemanth HM
Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.