Skip to content

Commit 2209343

Browse files
committed
Overhaul rubygems-basics (2022)
Signed-off-by: Takuya Noguchi <[email protected]>
1 parent 31ff160 commit 2209343

File tree

1 file changed

+82
-59
lines changed

1 file changed

+82
-59
lines changed

rubygems-basics.md

Lines changed: 82 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ next: /what-is-a-gem
1111
The `gem` command allows you to interact with RubyGems.
1212

1313
Ruby 1.9 and newer ships with RubyGems built-in but you may need to upgrade for
14-
bug fixes or new features. To upgrade RubyGems or install it for the first
15-
time (if you need to use Ruby 1.9) visit the
14+
bug fixes or new features. To upgrade RubyGems, visit the
1615
[download](https://rubygems.org/pages/download) page.
1716

1817
If you want to see how to require files from a gem, skip ahead to [What is a
@@ -37,11 +36,11 @@ expression characters in your query:
3736

3837
*** REMOTE GEMS ***
3938

40-
rails (4.0.0)
39+
rails (7.0.3)
4140
rails-3-settings (0.1.1)
41+
rails-acm (0.1.0)
4242
rails-action-args (0.1.1)
43-
rails-admin (0.0.0)
44-
rails-ajax (0.2.0.20130731)
43+
rails-action-authorization (1.1.2)
4544
[...]
4645

4746
If you see a gem you want more information on you can add the details option.
@@ -52,9 +51,9 @@ with details requires downloading more files:
5251

5352
*** REMOTE GEMS ***
5453

55-
rails (4.0.0)
54+
rails (7.0.3)
5655
Author: David Heinemeier Hansson
57-
Homepage: http://www.rubyonrails.org
56+
Homepage: https://rubyonrails.org
5857

5958
Full-stack web application framework.
6059

@@ -68,23 +67,23 @@ The `install` command downloads and installs the gem and any necessary
6867
dependencies then builds documentation for the installed gems.
6968

7069
$ gem install drip
71-
Fetching: rbtree-0.4.1.gem (100%)
72-
Building native extensions. This could take a while...
73-
Successfully installed rbtree-0.4.1
74-
Fetching: drip-0.0.2.gem (100%)
75-
Successfully installed drip-0.0.2
76-
Parsing documentation for rbtree-0.4.1
77-
Installing ri documentation for rbtree-0.4.1
78-
Parsing documentation for drip-0.0.2
79-
Installing ri documentation for drip-0.0.2
70+
Fetching drip-0.1.1.gem
71+
Fetching rbtree-0.4.5.gem
72+
Building native extensions. This could take a while...
73+
Successfully installed rbtree-0.4.5
74+
Successfully installed drip-0.1.1
75+
Parsing documentation for rbtree-0.4.5
76+
Installing ri documentation for rbtree-0.4.5
77+
Parsing documentation for drip-0.1.1
78+
Installing ri documentation for drip-0.1.1
8079
Done installing documentation for rbtree, drip after 0 seconds
8180
2 gems installed
8281

8382
Here the drip command depends upon the rbtree gem which has an extension. Ruby
8483
installs the dependency rbtree and builds its extension, installs the drip gem,
8584
then builds documentation for the installed gems.
8685

87-
You can disable documentation generation using the `--no-doc` argument when
86+
You can disable documentation generation using the `--no-document` argument when
8887
installing gems.
8988

9089
Requiring code
@@ -93,31 +92,47 @@ Requiring code
9392
RubyGems modifies your Ruby load path, which controls how your Ruby code is
9493
found by the `require` statement. When you `require` a gem, really you're just
9594
placing that gem's `lib` directory onto your `$LOAD_PATH`. Let's try this out
96-
in `irb` and get some help from the `pretty_print` library included with Ruby.
95+
in `irb`.
9796

98-
*Tip: Passing `-r` to
99-
`irb` will automatically require a library when irb is loaded.*
100-
101-
% irb -rpp
102-
>> pp $LOAD_PATH
103-
[".../lib/ruby/site_ruby/1.9.1",
97+
% irb
98+
irb(main):001:0> pp $LOAD_PATH
99+
[".../lib/ruby/site_ruby/3.1.0",
100+
".../lib/ruby/site_ruby/3.1.0/x86_64-linux",
104101
".../lib/ruby/site_ruby",
105-
".../lib/ruby/vendor_ruby/1.9.1",
102+
".../lib/ruby/vendor_ruby/3.1.0",
103+
".../lib/ruby/vendor_ruby/3.1.0/x86_64-linux",
106104
".../lib/ruby/vendor_ruby",
107-
".../lib/ruby/1.9.1",
108-
"."]
105+
".../lib/ruby/3.1.0",
106+
".../lib/ruby/3.1.0/x86_64-linux"]
109107

110108
By default you have just a few system directories on the load path and the Ruby
111109
standard libraries. To add the awesome_print directories to the load path,
112110
you can require one of its files:
113111

114-
% irb -rpp
115-
>> require 'ap'
112+
$ gem install awesome_print
113+
[...]
114+
$ irb
115+
irb(main):001:0> require "ap"
116116
=> true
117-
>> pp $LOAD_PATH.first
118-
".../gems/awesome_print-1.0.2/lib"
119-
120-
Note: For Ruby 1.8 you must `require 'rubygems'` before requiring any gems.
117+
irb(main):002:0> pp $LOAD_PATH.first
118+
".../gems/awesome_print-1.9.2/lib"
119+
120+
*Tip: Passing `-r` to `irb` will automatically require a library when irb is
121+
loaded.*
122+
123+
$ irb -rap
124+
irb(main):001:0> ap $LOAD_PATH
125+
[
126+
[0] ".../bundle/gems/awesome_print-1.9.2/lib",
127+
[1] ".../lib/ruby/site_ruby/3.1.0",
128+
[2] ".../lib/ruby/site_ruby/3.1.0/x86_64-linux",
129+
[3] ".../lib/ruby/site_ruby",
130+
[4] ".../lib/ruby/vendor_ruby/3.1.0",
131+
[5] ".../lib/ruby/vendor_ruby/3.1.0/x86_64-linux",
132+
[6] ".../lib/ruby/vendor_ruby",
133+
[7] ".../lib/ruby/3.1.0",
134+
[8] ".../lib/ruby/3.1.0/x86_64-linux"
135+
]
121136

122137
Once you've required `ap`, RubyGems automatically places its
123138
`lib` directory on the `$LOAD_PATH`.
@@ -150,36 +165,45 @@ The `list` command shows your locally installed gems:
150165

151166
*** LOCAL GEMS ***
152167

153-
bigdecimal (1.2.0)
154-
drip (0.0.2)
155-
io-console (0.4.2)
156-
json (1.7.7)
157-
minitest (4.3.2)
158-
psych (2.0.0)
159-
rake (0.9.6)
160-
rbtree (0.4.1)
161-
rdoc (4.0.0)
162-
test-unit (2.0.0.0)
168+
abbrev (default: 0.1.0)
169+
awesome_print (1.9.2)
170+
base64 (default: 0.1.1)
171+
benchmark (default: 0.2.0)
172+
bigdecimal (default: 3.1.1)
173+
bundler (default: 2.3.7)
174+
cgi (default: 0.3.1)
175+
csv (default: 3.2.2)
176+
date (default: 3.2.2)
177+
debug (1.4.0)
178+
delegate (default: 0.2.0)
179+
did_you_mean (default: 1.6.1)
180+
digest (default: 3.1.0)
181+
drb (default: 2.1.0)
182+
drip (0.1.1)
183+
english (default: 0.7.1)
184+
[...]
163185

164-
(Ruby ships with some gems by default, bigdecimal, io-console, json, minitest,
165-
psych, rake, rdoc, test-unit for ruby 2.0.0).
186+
The list includes defaults gems and bundled gems both of which were shipped
187+
with Ruby by default. In Ruby 3.1, the default gems are 70 gems in total
188+
including bigdecimal, bundler, csv, did_you_mean etc. and the bundled gems are
189+
debug, rake etc.
166190

167191
Uninstalling Gems
168192
-----------------
169193

170194
The `uninstall` command removes the gems you have installed.
171195

172196
$ gem uninstall drip
173-
Successfully uninstalled drip-0.0.2
197+
Successfully uninstalled drip-0.1.1
174198

175199
If you uninstall a dependency of a gem RubyGems will ask you for confirmation.
176200

177201
$ gem uninstall rbtree
178202

179203
You have requested to uninstall the gem:
180-
rbtree-0.4.1
204+
rbtree-0.4.5
181205

182-
drip-0.0.2 depends on rbtree (>= 0)
206+
drip-0.1.1 depends on rbtree (>= 0)
183207
If you remove this gem, these dependencies will not be met.
184208
Continue with Uninstall? [yN] n
185209
ERROR: While executing gem ... (Gem::DependencyRemovalException)
@@ -191,14 +215,14 @@ Viewing Documentation
191215
You can view the documentation for your installed gems with `ri`:
192216

193217
$ ri RBTree
194-
RBTree < MultiRBTree
218+
= RBTree < MultiRBTree
219+
220+
(from gem rbtree-0.4.5)
221+
------------------------------------------------------------------------
222+
A sorted associative collection that cannot contain duplicate keys.
223+
RBTree is a subclass of MultiRBTree.
224+
------------------------------------------------------------------------
195225

196-
(from gem rbtree-0.4.0)
197-
-------------------------------------------
198-
A sorted associative collection that cannot
199-
contain duplicate keys. RBTree is a
200-
subclass of MultiRBTree.
201-
-------------------------------------------
202226

203227
Fetching and Unpacking Gems
204228
---------------------------
@@ -211,7 +235,6 @@ If you wish to audit a gem's contents without installing it you can use the
211235
Fetching: malice-13.gem (100%)
212236
Downloaded malice-13
213237
$ gem unpack malice-13.gem
214-
Fetching: malice-13.gem (100%)
215238
Unpacked gem: '.../malice-13'
216239
$ more malice-13/README
217240

@@ -228,9 +251,9 @@ You can also unpack a gem you have installed, modify a few files, then use the
228251
modified gem in place of the installed one:
229252

230253
$ gem unpack rake
231-
Unpacked gem: '.../rake-10.1.0'
232-
$ vim rake-10.1.0/lib/rake/...
233-
$ ruby -I rake-10.1.0/lib -S rake some_rake_task
254+
Unpacked gem: '.../13.0.6'
255+
$ vim 13.0.6/lib/rake/...
256+
$ ruby -I 13.0.6/lib -S rake some_rake_task
234257
[...]
235258

236259
The `-I` argument adds your unpacked rake to the ruby `$LOAD_PATH` which

0 commit comments

Comments
 (0)