Skip to content

Instantly share code, notes, and snippets.

@spangenberg
Created February 6, 2011 13:40
Show Gist options
  • Save spangenberg/813374 to your computer and use it in GitHub Desktop.
Save spangenberg/813374 to your computer and use it in GitHub Desktop.
This script generates a matrix from a file with names
# To run:
#
# Put this script in a folder with a file named "names.txt"
# The File has the format:
# Daniel Spangenberg
# Toni Stark
# Iron Man
# ...
#
# Then run:
# ruby matrix.rb CELLS SPACE SPEED
# Example:
# ruby matrix.rb 59 2 0.19
class Matrix
class Cell
def initialize(name)
@position = -1
@name = name
end
def character
@position = @position + 1
@name[@position]
end
end
attr_accessor :names
def initialize(cells, spacing, speed)
@names = Array.new
@cells = Array.new(cells.to_i)
@spacing = spacing.to_i
@speed = speed.to_f
end
def random_name
names[rand(names.size)]
end
def new_cell
Cell.new(random_name)
end
def run
loop do
@cells.map! do |cell|
print " " * @spacing
if cell.nil? && rand(2) == 1
cell = new_cell
end
unless cell.nil?
char = cell.character
if char.nil?
print " "
nil
else
print char
cell
end
else
nil
print " "
end
end
puts
sleep(@speed)
end
end
end
matrix = Matrix.new(ARGV[0], ARGV[1], ARGV[2])
File.open("names.txt", "r") do |infile|
while (line = infile.gets)
matrix.names.push line.split.map { |name| name.capitalize }.join(" ")
end
end
matrix.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment