Skip to content

Instantly share code, notes, and snippets.

@rkulla
Last active September 10, 2016 04:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkulla/4700041 to your computer and use it in GitHub Desktop.
Save rkulla/4700041 to your computer and use it in GitHub Desktop.
Easily find and view Ruby standard library source code
#!/usr/bin/env ruby
# vsrcrb.rb by Ryan Kulla (http://rkulla.com).
# Note that it only works on a *nix OS (OSX, Linux, etc). Not Windows.
# Command-line program to output standard library module source
# Install: $ mkdir ~/bin ; mv -i vsrcrb.rb ~/bin/vsrcrb ; chmod u+x ~/bin/vsrcrb
# Usage: $ vsrcrb <module-name-without-extension>
# Example: View socket.rb's source code: $ vsrcrb socket | less
# To view a gem's src: $ less $(gem which <gem>)
def get_source_paths(target_file)
$LOAD_PATH.each do |dir_path|
Dir.chdir(dir_path) do
file_path = %x[#{"find `pwd` -iname #{target_file}.rb"}]
return file_path.split unless file_path == ''
end
end
end
def view_source(file_path)
File.foreach(file_path.chomp) do |line|
puts line
end
end
if $0 == __FILE__
found_paths = get_source_paths(ARGV[0])
puts 'Found:'
puts found_paths
puts
shortest_path = found_paths.min_by(&:length)
puts "Opening: #{shortest_path}\n\n"
40.times { print '=' }
puts
view_source(shortest_path)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment