mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Install And Require Gems Inline Without Gemfile as a Ruby TIL
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||
|
||||
_1701 TILs and counting..._
|
||||
_1702 TILs and counting..._
|
||||
|
||||
See some of the other learning resources I work on:
|
||||
|
||||
@@ -1402,6 +1402,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
||||
- [Iterate With An Offset Index](ruby/iterate-with-an-offset-index.md)
|
||||
- [Include Extra Context In A Honeybadger Notify](ruby/include-extra-context-in-a-honeybadger-notify.md)
|
||||
- [Ins And Outs Of Pry](ruby/ins-and-outs-of-pry.md)
|
||||
- [Install And Require Gems Inline Without Gemfile](ruby/install-and-require-gems-inline-without-gemfile.md)
|
||||
- [Install Latest Version Of Ruby With asdf](ruby/install-latest-version-of-ruby-with-asdf.md)
|
||||
- [Invoking Rake Tasks Multiple Times](ruby/invoking-rake-tasks-multiple-times.md)
|
||||
- [IRB Has Built-In Benchmarking With Ruby 3](ruby/irb-has-built-in-benchmarking-with-ruby-3.md)
|
||||
|
||||
74
ruby/install-and-require-gems-inline-without-gemfile.md
Normal file
74
ruby/install-and-require-gems-inline-without-gemfile.md
Normal file
@@ -0,0 +1,74 @@
|
||||
# Install And Require Gems Inline Without Gemfile
|
||||
|
||||
[Bundler](https://bundler.io/) has an _inline_ feature where you can declare
|
||||
gems that should be installed and required for the current file without the use
|
||||
of a `Gemfile`. This is useful for creating a single-file Ruby script that can
|
||||
define its own dependencies.
|
||||
|
||||
Require `"bundler/inline"` and then add a `gemfile` block toward the top of the
|
||||
script to specify the source and any gems.
|
||||
|
||||
```ruby
|
||||
require "bundler/inline"
|
||||
|
||||
gemfile do
|
||||
source "https://rubygems.org"
|
||||
gem "httparty"
|
||||
end
|
||||
```
|
||||
|
||||
When the script gets run (e.g. `ruby script.rb`), it will install the gems (if
|
||||
they haven't already been installed) and then run the script. You can specify
|
||||
version constraints just like you'd do in a `Gemfile`.
|
||||
|
||||
Here is a single-file script using this approach that I wrote to interact with
|
||||
the Kit API.
|
||||
|
||||
```ruby
|
||||
#!/usr/bin/env ruby
|
||||
require "bundler/inline"
|
||||
|
||||
gemfile do
|
||||
source "https://rubygems.org"
|
||||
gem "httparty"
|
||||
end
|
||||
|
||||
require "json"
|
||||
require_relative "kit_client"
|
||||
|
||||
API_SECRET = ENV["KIT_API_SECRET"]
|
||||
|
||||
def fetch_all_tags(api_secret)
|
||||
client = KitClient.new("https://api.kit.com/v4", api_secret)
|
||||
|
||||
tags = []
|
||||
after_cursor = nil
|
||||
|
||||
loop do
|
||||
params = {per_page: 1000}
|
||||
params[:after] = after_cursor if after_cursor
|
||||
|
||||
response = client.get("/tags", params)
|
||||
|
||||
data = JSON.parse(response.body)
|
||||
tags.concat(data["tags"])
|
||||
|
||||
break unless data["pagination"]["has_next_page"]
|
||||
after_cursor = data["pagination"]["end_cursor"]
|
||||
end
|
||||
|
||||
tags
|
||||
end
|
||||
|
||||
tags = fetch_all_tags(API_SECRET)
|
||||
|
||||
tags.each do |tag|
|
||||
puts tag
|
||||
end
|
||||
```
|
||||
|
||||
Because I've specified the shebang at the top of the file (and assuming I've
|
||||
`chmod +x fetch_tags.rb`), I can run this directly as a script with
|
||||
`./fetch_tags.rb`.
|
||||
|
||||
[source](https://bundler.io/guides/bundler_in_a_single_file_ruby_script.html)
|
||||
Reference in New Issue
Block a user