Sha256: 65d4f0a6521c131b75223d99e54ac6394b48e4044c0372076f80ade2dd07d5d8
Contents?: true
Size: 1.54 KB
Versions: 1
Compression:
Stored size: 1.54 KB
Contents
module Babble #Slurp argument files/standard input and output scrambled text. def Babble.main print scramble_text(gets(nil)) end #Split lines of text into 'words'. (A 'word' may include formatting characters other than a space.) #text.each should call a block with each line of text. def Babble.parse_words(text) words = [] text.each do |line| line.split(/ /).each {|word| words << word} end words end #Scramble a list of items. #odds is the likelihood that on encountering an item, it will jump to another occurrence of the same item. def Babble.scramble (list, odds = 10) #Build index of items. item_indices = Hash.new {|h, k| h[k] = Array.new} list.each_with_index {|item, index| item_indices[item].push(index)} #Loop through items, occasionally jumping to random occurrence of same item. index = 0 scrambled_list = [] while (index < list.length) #Get current item. item = list[index] #Add it to scrambled list. scrambled_list << item #If we should jump to another instance of the same item... if (rand(odds) >= odds - 1) #Choose a random occurrence of the same item. occurrence = rand(item_indices[item].length) index = item_indices[item][occurrence] end #Move to next item. index += 1 end scrambled_list end #Scramble the given text. def Babble.scramble_text(text, odds = 10) scrambled_text = "" scramble(parse_words(text)).each do |word| scrambled_text += word scrambled_text += " " unless word =~ /\n$/ end scrambled_text end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
babble-1.0.1 | lib/babble.rb |