This repository was archived by the owner on Aug 26, 2019. It is now read-only.
forked from adamwiggins/rush
-
Notifications
You must be signed in to change notification settings - Fork 8
Handbook: File Contents
Naja Melan edited this page May 2, 2014
·
6 revisions
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_dirUse "entries" to get just the list of files, or "lines" to get the line matches.
dir['**/*'].search(/describe/).linesBetter 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."Previous: Shell - Next: Permissions
source: https://web.archive.org/web/http://rush.heroku.com/handbook/file_contents