1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Search For Gem Versions Available To Install as a Ruby TIL

This commit is contained in:
jbranchaud
2023-10-11 08:53:51 -05:00
parent 9bcbcbc7c0
commit ee3c671c47
2 changed files with 35 additions and 1 deletions

View File

@@ -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)

View File

@@ -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.