Midwire

Ruby, Elixir, Macintosh and Other Technical Maunder.

A Useful ~/.irbrc File

Here is my ~/.irbrc file.

~/.irbrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
require 'irb/completion'

def what_methods(obj)
  (obj.public_methods - Object.public_methods - Object.instance_methods).sort
end

def wm(obj)
  what_methods(obj)
end

def predicates(obj)
  what_methods(obj).grep(/\?$/)
end

def locate(obj, method)
  obj.method(method.to_sym).source_location
end

It contains 4 useful methods as follows:

what_methods

Allows you to determine what methods are available on an object without displaying all of Object instance methods. The following irb session calls what_methods on an Array instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ary = [1,2,3]
what_methods ary
=> [:&,
 :*,
 :+,
 :-,
 :<<,
 :[],
 :[]=,
 :all?,
   # ...
 :uniq,
 :uniq!,
 :unshift,
 :values_at,
 :zip,
 :|]

Note: wm is just a shortcut for what_methods, cuz I’m lazy.

As you can see, it lists out all public instance methods for the Array instance, ary.

predicates

Predicates is similar to what_methods. It actually calls what_methods and filters out all but methods that end with a question mark (predicates).

1
2
3
ary = [1,2,3]
predicates ary
=> [:all?, :any?, :empty?, :member?, :none?, :one?]

Pretty cool, huh?

locate(obj, method)

Have you ever been at some breakpoint and wanted to look at the code for a specific method? This tiny method lets you do just that.

1
2
3
get :index
locate response, :ok?
["/Users/midwire/Code/ruby/Rallyhood/rh_svc_analytics/vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/response.rb", 126]

How about that? It even gives you the line number along with the file where the method is defined.

To use these in an irb session, just copy them into your ~/.irbrc file. However, that won’t help if you plan to use them during a pry session. For that just copy them into your ~/.pryrc file.

Enjoy!

Comments