use bundler with a single file
How to use bundler with a single file?
I normal way, It’s very easy to create a ruby script and use the desired gem by just requiring then in your ruby script.
And if you are using many external gems you can easily define the Gemfile
using bundle init
and add the gem dependency in it and use it in your ruby script.
sometimes you are too lazy and don’t want to define the Gemfile
and want everything in the same file. So there is a bundler/inline
module which gives you the power to define all your gem dependency in the ruby script file itself.
You can use Bundler in a single-file script, just require 'bundler/inline'
at the top of your Ruby file and hen, use the gemfile method to declare any gem sources and gems.
Below is a simple snippet example
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'actionpack', '~> 5.2', '>= 5.2.1'
gem 'rails'
gem 'nokogiri'
end
require 'open-uri'
page = open("https://www.tenderprog.com/")
html = Nokogiri::HTML page
head_element = html.xpath("/html/head/title")
puts head_element