Skip to content

Instantly share code, notes, and snippets.

@tkareine
Created January 27, 2011 11:35
Show Gist options
  • Save tkareine/798394 to your computer and use it in GitHub Desktop.
Save tkareine/798394 to your computer and use it in GitHub Desktop.
Create random plain text file
# 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