full text searching for ri content

| | Comments (3)

UPDATE: See the latest version.

Check it out. Quick and dirty searching of ri content:

#!/usr/local/bin/ruby -w

require 'find'
require 'yaml'

search = ARGV.shift

puts "Searching for #{search}"
puts

Dir.chdir '/usr/local/share/ri/1.8/system' do
  Find.find('.') do |path|
    next unless test ?f, path
    yaml = File.read path
    if yaml =~ /#{search}/io then
      full_name = $1 if yaml[/full_name: (.*)/]
      puts "** FOUND IN: #{full_name}"
      
      data = YAML.load yaml.gsub(/ \!.*/, '')
      desc = data['comment'].map { |x| x.values }.flatten.join("\n").gsub(/"/, "'").gsub(/</, "<").gsub(/>/, ">").gsub(/&/, "&")
      puts
      puts desc
      puts
    end
  end
end
Lets you do stuff like:
% ./risearch.rb duplicate
Searching for duplicate
[...]
** FOUND IN: Array#uniq!

Removes duplicate elements from self. Returns nil if no changes are made (that is, no duplicates are found).
   a = [ 'a', 'a', 'b', 'b', 'c' ]
   a.uniq!   #=> ['a', 'b', 'c']
   b = [ 'a', 'b', 'c' ]
   b.uniq!   #=> nil

** FOUND IN: Array#|

Set Union---Returns a new array by joining this array with other_array, removing duplicates.
   [ 'a', 'b', 'c' ] | [ 'c', 'd', 'a' ]
          #=> [ 'a', 'b', 'c', 'd' ]
[...]

3 Comments

Cool! To get it to work on Windows, I added require 'rdoc/ri/ri_paths' then replaced Dir.chdir '/usr/local/share/ri/1.8/system' do with Dir.chdir RI::Paths::PATH[0] do

I made some small mods to it to completely replace the standard 'ri' wrapper and so it would search through the gem installed ri docs, too.

http://blog.360.yahoo.com/blog-N.f1XKsnfqMNcewZyWk-?cq=1&p=27

what an ugly url. :)

muahahaha!!!

I beat you to it... I just didn't blog it. done.

Leave a comment