How to search Ruby files in Windows

Sometimes you want to find some particular code snippet and all you know is that it is in some file in a given directory. For example, if you are reading the Pickaxe book and you want to actually run some example code, all you know a priori is that it is somewhere in ex0001.rb through ex1608.rb. I use a Mac and the wonderful Spotlight Ruby Importer available at:
<http://www.arcadianvisions.com/>
makes it trivial to find a file containing any given code snippet. I have a friend who uses Windows and she doesn't know how to achieve the same functionality. I've tried running RDoc on the directory containing the Pickaxe code but it quits because it only works on file that actually compile.

So all this is simply to ask: What is the Windows equivalent to the Spotlight Ruby Importer.

···

--
A young idea is a beautiful and a fragile thing. Attack people, not ideas.

Chris Gehlker wrote:

So all this is simply to ask: What is the Windows equivalent to the
Spotlight Ruby Importer.

Wow, that's sort of sexy. Hadn't seen that before.
(Screenshots in action: http://rubyurl.com/9sx )

The Windows Way might be to use the Find command (Windows-F) and enter
content type in "A word or phrase in the file". Bleah.

What I do is use my little "findfile" ruby script (code below) from the
command line. It's sort of like a grep utility that allows you to use
regexps as a filter for file names, and as a content searcher. For
example

C:\Documents and Settings\gavin.kistner\Desktop>findfile rbw?$ "def
\S+awl"
./ReArchive/Archive/dircrawl.rb
        def self.crawl ( path, level=0 )

Found 1 file (out of 7510) in 2.765 seconds

C:\WINDOWS\system32>type findfile.rb
require_gem 'usage'
usage = Usage.new "[-d %max_depth] name_regexp [content_regexp]"

class Dir
  def self.crawl( path, max_depth=nil, depth=0, &block )
    return if max_depth && depth > max_depth
    begin
      if File.directory?( path )
        files = Dir.entries( path ).select{ |f| f[0,1]!='.' }
        unless files.empty?
          files.collect!{ |file_path|
            Dir.crawl( path+'/'+file_path, max_depth, depth+1, &block )
          }.flatten!
        end
        return files
      elsif File.file?( path )
        yield( path, depth )
      end
    rescue SystemCallError => the_error
      warn "ERROR: #{the_error}"
    end
  end
end

start_time = Time.new
name_match = Regexp.new( usage.name_regexp, true )
content_match = usage.content_regexp && Regexp.new(
".{0,10}#{usage.content_regexp}.+", true )

file_count = 0
matching_count = 0
Dir.crawl( '.', usage.max_depth ){ |file_path, depth|
  if File.split( file_path )[ 1 ] =~ name_match
    if content_match
      if IO.read( file_path ) =~ content_match
        puts file_path," #{$~}"," "
        matching_count += 1
      end
    else
      puts file_path
      matching_count += 1
    end
  end
  file_count += 1
}
elapsed = Time.new - start_time

puts "Found #{matching_count} file#{matching_count==1?'':'s'} (out of
#{file_count}) in #{elapsed} seconds"

Chris Gehlker wrote:

Sometimes you want to find some particular code snippet and all you know is that it is in some file in a given directory. For example, if you are reading the Pickaxe book and you want to actually run some example code, all you know a priori is that it is somewhere in ex0001.rb through ex1608.rb. I use a Mac and the wonderful Spotlight Ruby Importer available at:
<http://www.arcadianvisions.com/&gt;
makes it trivial to find a file containing any given code snippet. I have a friend who uses Windows and she doesn't know how to achieve the same functionality. I've tried running RDoc on the directory containing the Pickaxe code but it quits because it only works on file that actually compile.

So all this is simply to ask: What is the Windows equivalent to the Spotlight Ruby Importer.

--
A young idea is a beautiful and a fragile thing. Attack people, not ideas.

I use cygwin (www.cygwin.com) and something like:
( mod to your liking, or make different versions for different needs)

cyberhome: /home/rthompso/bin>
$ cat greprb
if [ $# = 1 ]
then
    dir=.
    echo "dir is $dir"
else if [ $# = 2 ]
then
    dir=$2
    echo "dir is $dir"
else
    echo "Usage: 'basename $0' pattern [path]"
    echo "don't forget to quote patterns with spaces"
    echo "\n"
exit 1
fi

find $dir -type f -name \*.rb -exec grep -l "$1" {} \; -exec grep -n "$1" {} \; -exec echo " " \;

···

==============================================================================
Example output ( filepath and line of occurance ):

$ greprb require .
dir is .
./.vim/plugin/snippetMagic/convert.rb
3:require 'rexml/document'
4:require 'rexml/streamlistener'
5:require 'yaml'
7:require 'snippet_set'
./.vim/plugin/snippetMagic/snippets/php.rb
11: - require_once(..) (req1)
12: - "require_once( '${1:file}' );$0"
69: - require(..) (req)
70: - "require( '${1:file}' );$0"
./.vim/plugin/snippetMagic/snippets/propel.rb
16: - "<column name=\"${1:name}\" type=\"${2:type}\"${3: required=\"${4:true}\"}${5:
20: - "<column name=\"${1:id}\" type=\"${2:integer}\" required=\"true\"
./.vim/plugin/snippetMagic/snippet_set.rb
1:require 'yaml'
39: # make sense to require installation of the gem for a vim plugin.
./client.rb
2:require "socket"
./date.rb
1:require 'date'
./dbtest.rb
1:require 'dbi'
./eXPlainPMT-20060408.0/app/controllers/application.rb
38: before_filter :require_team_membership
70: # This method can be used as a before_filter for actions which should require
75: def require_admin_privileges
87: def require_team_membership
123: def require_current_project
132: flash[:error] = "You attempted to access a view that requires a " +
./eXPlainPMT-20060408.0/app/controllers/iterations_controller.rb
22: before_filter :require_current_project
./eXPlainPMT-20060408.0/app/controllers/milestones_controller.rb
22: before_filter :require_current_project, :except => [:milestones_calendar]

Can you give us the non-RubyURL link, since the service seems to be down currently?

James Edward Gray II

···

On Aug 22, 2006, at 12:30 PM, Phrogz wrote:

Chris Gehlker wrote:

So all this is simply to ask: What is the Windows equivalent to the
Spotlight Ruby Importer.

Wow, that's sort of sexy. Hadn't seen that before.
(Screenshots in action: http://rubyurl.com/9sx )

Thanks Phrogz, I'll pass it on right now.

···

On Aug 22, 2006, at 10:30 AM, Phrogz wrote:

The Windows Way might be to use the Find command (Windows-F) and enter
content type in "A word or phrase in the file". Bleah.

What I do is use my little "findfile" ruby script (code below) from the
command line. It's sort of like a grep utility that allows you to use
regexps as a filter for file names, and as a content searcher. For
example

C:\Documents and Settings\gavin.kistner\Desktop>findfile rbw?$ "def
\S+awl"
./ReArchive/Archive/dircrawl.rb
        def self.crawl ( path, level=0 )

Found 1 file (out of 7510) in 2.765 seconds

--
Conscience is thoroughly well-bred and soon leaves off talking to those who do not wish to hear it.
-Samuel Butler, writer (1835-1902)

Phrogz wrote:

What I do is use my little "findfile" ruby script (code below) from the
command line. It's sort of like a grep utility that allows you to use
regexps as a filter for file names, and as a content searcher. For

I have a little script called "fic" ... "Find In Code". Here it is:

  #!/usr/bin/env ruby
  require 'rake'
  FileList["**/*.rb"].egrep(Regexp.new(ARGV.first))

-- Jim Weirich

···

--
Posted via http://www.ruby-forum.com/\.

Phrogz wrote:

The Windows Way might be to use the Find command (Windows-F) and enter
content type in "A word or phrase in the file". Bleah.

Actually, I think Windowsen have a document indexing service hidden somewhere. Hard to believe it wouldn't do as much as fulltext document indexing. And it's also probably extensible if you know the proper hand motions and undocumented API calls. Maybe.

Buggered if I know how to get it to work, and the last time I let the system index hard drive contents, it hogged the drives to no end. But it is a hint to Windows hackers that would find this an interesting problem to solve.

David Vallner

Chris Gehlker wrote:

So all this is simply to ask: What is the Windows equivalent to the
Spotlight Ruby Importer.

Wow, that's sort of sexy. Hadn't seen that before.
(Screenshots in action: http://rubyurl.com/9sx )

Can you give us the non-RubyURL link, since the service seems to be down currently?

Although the rubyurl link worked for me just now, here's
what it resolved to:

http://www.arcadianvisions.com/images/RubyMethodSearch.html

HTH,

Bill

···

From: "James Edward Gray II" <james@grayproductions.net>

On Aug 22, 2006, at 12:30 PM, Phrogz wrote:

That's pretty cool too.

···

On Aug 22, 2006, at 12:03 PM, Jim Weirich wrote:

I have a little script called "fic" ... "Find In Code". Here it is:

  #!/usr/bin/env ruby
  require 'rake'
  FileList["**/*.rb"].egrep(Regexp.new(ARGV.first))

--
No matter how far you have gone on the wrong road, turn back.
  -Turkish proverb

Not a good one. Use Google Desktop, or Yahoo Desktop, or MSN Desktop.

I use the former, when I'm on Windows.

-austin

···

On 8/24/06, David Vallner <david@vallner.net> wrote:

Actually, I think Windowsen have a document indexing service hidden
somewhere. Hard to believe it wouldn't do as much as fulltext document
indexing. And it's also probably extensible if you know the proper hand
motions and undocumented API calls. Maybe.

--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
               * austin@halostatue.ca * You are in a maze of twisty little passages, all alike. // halo • statue
               * austin@zieglers.ca