Sunday, December 30, 2007

Ruby which method

Just thought this was kinda interesting, the question came up in the merb IRC channel.


<wycats> you know what would be helpful... a which command in Ruby that went through the path and figured out what the path of some require would be


So, Here's a starting point:

def which(file)
path = $:.detect{|p| File.exist?("#{p}/#{file}.rb") }
"#{path}/#{file}.rb" if path
end


This of course doesn't account for .so files or .bundle, but could be pretty handy to include in an irbrc for debugging.

1 comments:

Matt said...

any suggestions on a way to do something similar for a method? i.e. if I want to call a method in a particular context (where there's been subclassing or overridden methods), how can I tell which class/code will actually respond to that call?

Reading list