Skip to content

Commit ee3c671

Browse files
committed
Add Search For Gem Versions Available To Install as a Ruby TIL
1 parent 9bcbcbc commit ee3c671

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1341 TILs and counting..._
13+
_1342 TILs and counting..._
1414

1515
---
1616

@@ -1124,6 +1124,7 @@ _1341 TILs and counting..._
11241124
- [Safe Navigation Operator](ruby/safe-navigation-operator.md)
11251125
- [Scripting With RVM](ruby/scripting-with-rvm.md)
11261126
- [Scroll To Top Of Page With Capybara](ruby/scroll-to-top-of-page-with-capybara.md)
1127+
- [Search For Gem Versions Available To Install](ruby/search-for-gem-versions-available-to-install.md)
11271128
- [Set RVM Default Ruby](ruby/set-rvm-default-ruby.md)
11281129
- [Shift The Month On A Date Object](ruby/shift-the-month-on-a-date-object.md)
11291130
- [Show Public Methods With Pry](ruby/show-public-methods-with-pry.md)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Search For Gem Versions Available To Install
2+
3+
The [`gem list`](https://guides.rubygems.org/command-reference/#gem-list)
4+
command combined with a few flags will produce a listing of all available
5+
versions of that gem like so:
6+
7+
```bash
8+
gem list rails --exact --remote --all
9+
10+
*** REMOTE GEMS ***
11+
12+
rails (7.1.0, 7.0.8, 7.0.7.2, 7.0.7.1, 7.0.7, ...)
13+
```
14+
15+
I can then apply a bit of command-line transformation with `sed` and `tr` to
16+
turn that list of version numbers into a list that can be nicely consumed by
17+
other commands. In particular, I will pipe that list to `fzf` so that I can
18+
fuzzy-search through the huge list for specific version matches.
19+
20+
```bash
21+
$ gem list rails --exact --remote --all \
22+
| sed -n 's/.*(\([^)]*\)).*/\1/p' \
23+
| tr ',' '\n' \
24+
| sed 's/^ //' \
25+
| fzf
26+
```
27+
28+
The first `sed` command captures everything inside the parentheses. The `tr`
29+
command replaces the commas with new lines. And the second `sed` command
30+
removes those leading space on each line.
31+
32+
Lastly, [`fzf`](https://github.com/junegunn/fzf) provides a fuzzy-search
33+
interface over the list of versions.

0 commit comments

Comments
 (0)