20Oct/080
Filter Listings of an Object’s Methods in IRB
So the next stop after sorting an object's methods in irb, is to be able to filter it.
Why? Well I roughly know the method name, so how about filtering down that big list of 163 methods names to something smaller? like say 5?
The traditional way to do it is (yes without the change in my last post):
object.methods.sort.select { |a| a =~ /rev/ }
Damn that's one long line. Even with my change, it reduces it by only 5 characters.
So what is one to do? Try popping this into your ~/.irbrc:
class Object
def methods_with_sort(filter = nil)
methods = methods_without_sort
if filter
filter = Regexp.new(filter.to_s, true) unless filter.is_a? Regexp
methods = methods.select { |method| method =~ filter }
end
methods.sort
end
alias_method :methods_without_sort, :methods
alias_method :methods, :methods_with_sort
end
Now we can do this:
object.methods :rev
The World is Just Awesome.
