Handbook: File Contents
Read and write file contents easily:
dir['Rakefile'].contents
dir['Rakefile'].write "# overwritten from rush\n"
Search (aka grep) the files, but without the confusing differences between grep, egrep, and Ruby regular expressions:
dir['*.rb'].search(/describe Rush::[A-Z][a-z]+/ do)
The results of a search can be used as input for another search - or can be used with any other command.
dir['*.rb'].search(/^\s*class/).search(/Rush/).copy_to other_dir
Use "entries" to get just the list of files, or "lines" to get the line matches.
dir['**/*'].search(/describe/).lines
Better yet, search and replace. How about changing all your tabs to spaces?
myproj['**/*.rb'].replace_contents!(/\t/, ' ')
Doing some refactoring?
myproj['**/*.rb'].replace_contents!(/OldClass/, 'NewClass')
Don't forget - this is Ruby!
spec_lines = myproj['**/*_spec.rb'].line_count
total_lines = myproj['**/*.rb'].line_count
spec_percent = (spec_lines * 100 / total_lines).round
puts "Specs account for #{spec_percent}% of the total code."
puts (spec_percent >= 30) ? "Good for you!" : "Tsk, tsk."
