public
Created

Generate ascii strings

  • Download Gist
random_strings.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#!/usr/bin/env ruby -w
 
class BitString
 
attr_accessor :bit_string
 
def initialize string_length
@bit_string = random_bit_string string_length
end
 
def random_bit_string length
arr = Array.new length
length.times {|i| arr[i] = random_bit}
arr.join
end
 
def random_bit
rand(100) % 2
end
 
def to_ascii
arr = []
bit_string.scan(/[01]{7}/).each do |str|
arr << str.to_i(2).chr if str.length == 7
end
arr.join
end
end
class RandomStrings
 
attr_accessor :str_length, :str_count
 
def initialize string_length, string_count
@str_length = string_length
@str_count = string_count
end
 
def run
str_count.times do
p BitString.new(str_length).to_ascii
end
end
end
if __FILE__ == $0
length_of_string = ARGV[0].to_i
number_of_strings = ARGV[1].to_i
RandomStrings.new(length_of_string, number_of_strings).run
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.