Module: Gemma::Utility

Defined in:
lib/gemma/utility.rb

Overview

Utility functions.

Class Method Summary collapse

Class Method Details

If the usage information for a script is documented in a comment block at the top of the file, this method prints it out. If there is a shebang (#!) on the first line, it is not printed.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gemma/utility.rb', line 12

def self.print_usage_from_file_comment(
  file, comment_char = '#', io = $stdout
)
  lines = File.readlines(file)
  lines.shift if lines.first =~ /^#!/ # ignore shebang
  lines.each do |line|
    line.strip!
    break unless line =~ /^#{comment_char}(.*)/
    io.puts Regexp.last_match(1)
  end
end

.rgrep(tag, path, io = $stdout) ⇒ nil

A very simple recursive grep.

Parameters:

  • tag (Regexp)
  • path (String)
  • io (IO) (defaults to: $stdout)

Returns:

  • (nil)


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gemma/utility.rb', line 35

def self.rgrep(tag, path, io = $stdout)
  Dir.chdir path do
    globs = Dir['**/*'] + Dir['**/.*']
    globs.select { |f| File.file?(f) }.sort.each do |f|
      File.readlines(f).each_with_index do |line, i|
        io.puts "#{f}:#{i}: #{line}" if line =~ tag
      end
    end
  end
  nil
end