Module: Gemma::Utility
- Defined in:
- lib/gemma/utility.rb
Overview
Utility functions.
Class Method Summary collapse
-
.print_usage_from_file_comment(file, comment_char = '#', io = $stdout) ⇒ Object
If the usage information for a script is documented in a comment block at the top of the file, this method prints it out.
-
.rgrep(tag, path, io = $stdout) ⇒ nil
A very simple recursive grep.
Class Method Details
.print_usage_from_file_comment(file, comment_char = '#', io = $stdout) ⇒ Object
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 23 24 |
# File 'lib/gemma/utility.rb', line 12 def self.print_usage_from_file_comment( file, comment_char = '#', io = $stdout ) lines = File.readlines(file) # ignore shebang and magic comments lines.shift if lines.first =~ /^#!/ lines.shift if lines.first =~ /^#\s*frozen_string_literal/ 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.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/gemma/utility.rb', line 37 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 |