Created
January 27, 2011 11:35
-
-
Save tkareine/798394 to your computer and use it in GitHub Desktop.
Create random plain text file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
SIZE = ARGV.shift.to_i | |
FILENAME = ARGV.shift | |
if SIZE < 1 || FILENAME.nil? | |
puts "Usage: ruby #{File.basename(__FILE__)} <size> <file>" | |
exit 1 | |
end | |
WIDTH = 80 | |
NUM_FULL_LINES = SIZE / WIDTH | |
LAST_LINE_WIDTH = SIZE % WIDTH | |
CHARS = [('a'..'z'), ('A'..'Z'), ('0'..'9')].map { |r| r.to_a }.flatten.join | |
def create_random_line(width) | |
line = '' | |
(width - 1).times { line << CHARS[rand(CHARS.size)] } # minus one for newline | |
line << "\n" | |
end | |
File.open(FILENAME, 'w') do |file| | |
NUM_FULL_LINES.times { file.write(create_random_line(WIDTH)) } | |
file.write(create_random_line(LAST_LINE_WIDTH)) if LAST_LINE_WIDTH > 0 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment