1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#+TITLE: Lesser Known IRB Features
#+DATE: 2023-04-28T00:22:54-04:00
#+DRAFT: true
#+DESCRIPTION: Going over some of my favourite lesser known IRB features
#+TAGS[]: ruby
#+KEYWORDS[]: ruby
#+SLUG:
#+SUMMARY:
While poking around in IRB and looking at the autocompletion results,
I found some functions that I didn't recognize and decided to take a
look at what they do. I found some pretty interesting stuff.
It looks like some of these commands have been added recently to come
up to par with =pry=, and others have been kicking around for a while
and just flying under the radar.
You can find them all defined in [[https://github.com/ruby/irb/blob/master/lib/irb/extend-command.rb][this file]] in the IRB source code.
- IRB workspaces
- The terms workspace/binding/context are used interchangeably
- For some reason these methods have an excessive amount of aliases
- =pushb <obj>=, =pushws=, =irb_push_workspace=, =irb_push_binding=
change the current context (binding) to whatever object you
pass as arg. It pushes your binding onto the =workspaces= stack.
- =popb=, =popws=, =irb_pop_workspace=, =irb_push_binding= pop your
current workspace off the stack and take you out and bring you to
your previous binding environment.
- =cb [obj]=, =cws=, =chwd=, =irb_cb=, =irb_cws=, =irb_chws=,
=irb_change_binding= change the current context without pushing
the current context onto the stack, and change to the default
context when called without an argument.
- Examining object internals
- =ls [obj]= lists the contents of an object (methods, inherited
methods, instance variables, etc.)
- Switching between multiple IRB sessions (sub-IRBs)
- Running =irb= creates a sub-IRB.
- =jobs= lists existing sub-IRBs
- =fg <num>= lets you change sub-IRB
- =kill <num>= kills the given sub-IRB
- Find the source code for a method or constant
- =show_source <obj/method>= Displays the file path a method or
object was defined in, and prints its contents
- Read documentation
- =show_doc [method]= Either opens the documentation for the
constant or method provided as an argument, or opens an RI
documentation prompt of no argument is given.
- Show the current line
- =whereami= lists the current line of execution of the binding.
This is useful if you open IRB from =binding.irb= or =binding.b=
- List internal IRB commands
- =show_cmds= lists most of these commands inside of IRB :)
|