Carina C. Zona bio photo

Carina C. Zona

Email Twitter LinkedIn

Ingenious. Simple. If you program in Ruby, check out the handy-dandy MethodFinder utility. It's a prompt for your memory when you only almost remember which method name you want. Or when you have no idea altogether. You just give it a receiver and desired return value; it gives you a list of methods which, when sent to the receiver, return the specified value.

Install MethodFinder


$ gem install methodfinder

(The method_finder gem is unrelated)

Let the searches begin

If you require it in .irbrc, then irb sessions will have MethodFinder available automatically.


# .irbrc
begin; require 'methodfinder'; rescue LoadError => err; warn "#{err}"; end

Now let's fire up irb

$ irb
irb > MethodFinder.find('a', 0)
=> [:oct, :to_c, :to_f, :to_i, :to_r]
irb > MethodFinder.find('a', 1)
=> [:bytesize, :length, :size]
irb > MethodFinder.find('a', :a)
=> [:intern, :to_sym]
irb > MethodFinder.find('a', String)
=> [:class]
irb > MethodFinder.find(10, 100)
=> [:abs2]
irb > MethodFinder.find(10,11)
=> [:next, :succ]
irb > MethodFinder.find('abc', 'Abc')
=> [:capitalize, :capitalize!]
irb > MethodFinder.find('abc', 'cba')
=> [:reverse, :reverse!]
irb > MethodFinder.find('abc', 'a')
=> [:chr]
irb > MethodFinder.find('abc', 'ab')
=> [:chop, :chop!]
irb > MethodFinder.find('abc', :abc)
=> [:intern, :to_sym]
irb > MethodFinder.find('abc', 'ABC')
=> [:swapcase, :swapcase!, :upcase, :upcase!]
irb > MethodFinder.find(0, true)
=> [:even?, :frozen?, :integer?, :real?, :zero?]
irb > MethodFinder.find(0, false)
=> [:!, :nil?, :odd?, :tainted?, :untrusted?]

MethodFinder is looking for return value, so keep in mind that some methods don't have the return value that one might expect them to. For instance, IO#puts, IO#print, and IO#printf send to standard out (STDOUT) while returning nil.

Get More Elaborate

The documentation demonstrates that more complex and/or granular searches are possible too. You can inquire what method to use within a supplied block, for instance.