diff --git a/README.md b/README.md index cb4ca7e..a2f88ea 100644 --- a/README.md +++ b/README.md @@ -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). -_1341 TILs and counting..._ +_1342 TILs and counting..._ --- @@ -1124,6 +1124,7 @@ _1341 TILs and counting..._ - [Safe Navigation Operator](ruby/safe-navigation-operator.md) - [Scripting With RVM](ruby/scripting-with-rvm.md) - [Scroll To Top Of Page With Capybara](ruby/scroll-to-top-of-page-with-capybara.md) +- [Search For Gem Versions Available To Install](ruby/search-for-gem-versions-available-to-install.md) - [Set RVM Default Ruby](ruby/set-rvm-default-ruby.md) - [Shift The Month On A Date Object](ruby/shift-the-month-on-a-date-object.md) - [Show Public Methods With Pry](ruby/show-public-methods-with-pry.md) diff --git a/ruby/search-for-gem-versions-available-to-install.md b/ruby/search-for-gem-versions-available-to-install.md new file mode 100644 index 0000000..b272283 --- /dev/null +++ b/ruby/search-for-gem-versions-available-to-install.md @@ -0,0 +1,33 @@ +# Search For Gem Versions Available To Install + +The [`gem list`](https://guides.rubygems.org/command-reference/#gem-list) +command combined with a few flags will produce a listing of all available +versions of that gem like so: + +```bash +gem list rails --exact --remote --all + +*** REMOTE GEMS *** + +rails (7.1.0, 7.0.8, 7.0.7.2, 7.0.7.1, 7.0.7, ...) +``` + +I can then apply a bit of command-line transformation with `sed` and `tr` to +turn that list of version numbers into a list that can be nicely consumed by +other commands. In particular, I will pipe that list to `fzf` so that I can +fuzzy-search through the huge list for specific version matches. + +```bash +$ gem list rails --exact --remote --all \ + | sed -n 's/.*(\([^)]*\)).*/\1/p' \ + | tr ',' '\n' \ + | sed 's/^ //' \ + | fzf +``` + +The first `sed` command captures everything inside the parentheses. The `tr` +command replaces the commas with new lines. And the second `sed` command +removes those leading space on each line. + +Lastly, [`fzf`](https://github.com/junegunn/fzf) provides a fuzzy-search +interface over the list of versions.