module RujitsuInteger
Constants
- CHARACTERS
- CONSONANTS
- EVENS
- HEX_CHARACTERS
- LETTERS
- NUMBERS
- ODDS
- VOWELS
Public Instance Methods
random_characters(opts={})
click to toggle source
produce a string of N random characters
5.random_characters
# File lib/rujitsu/integer.rb 47 def random_characters opts={} 48 generate_random_string_using CHARACTERS, opts 49 end
Also aliased as: random_character
random_consonants(opts={})
click to toggle source
produce a string of N random consonants
# File lib/rujitsu/integer.rb 11 def random_consonants opts={} 12 generate_random_string_using CONSONANTS, opts 13 end
Also aliased as: random_consonant
random_hex_characters(opts={})
click to toggle source
# File lib/rujitsu/integer.rb 52 def random_hex_characters opts={} 53 generate_random_string_using HEX_CHARACTERS, opts 54 end
Also aliased as: random_hex_character
random_letters(opts={})
click to toggle source
produce a string of N random letters
5.random_letters
# File lib/rujitsu/integer.rb 18 def random_letters opts={} 19 generate_random_string_using LETTERS, opts 20 end
Also aliased as: random_letter
random_numbers( opts = {} )
click to toggle source
produce a string of N random numbers
5.random_numbers
optionally specific limits on the numbers
5.random_numbers(:from => 1, :to => 5)
# File lib/rujitsu/integer.rb 27 def random_numbers( opts = {} ) 28 # Then set some defaults, just in case 29 upper = opts[:to] || 9 30 lower = opts[:from] || 0 31 only = opts[:only] || :both 32 33 # And finally calculate the number 34 n = [] 35 (self - 1).times do 36 i = (lower..upper).to_a.sort_by { rand }.first 37 n << i.to_s 38 end 39 # add the last digit according to :only 40 n << end_number_choices(only).select {|x| (lower <= x) && (x <= upper) }.sort_by { rand }.first.to_s 41 n.join("") 42 end
Also aliased as: random_number
random_vowels(opts={})
click to toggle source
produce a string of N random vowels
# File lib/rujitsu/integer.rb 5 def random_vowels opts={} 6 generate_random_string_using VOWELS, opts 7 end
Also aliased as: random_vowel
Private Instance Methods
end_number_choices(opt)
click to toggle source
# File lib/rujitsu/integer.rb 87 def end_number_choices opt 88 case opt 89 when :even 90 EVENS 91 when :odd 92 ODDS 93 else 94 EVENS | ODDS 95 end 96 end
generate_random_string_using(legal_characters, opts={})
click to toggle source
# File lib/rujitsu/integer.rb 68 def generate_random_string_using(legal_characters, opts={}) 69 # Check if we have anything to exclude from the legal_characters 70 if (chars = opts[:except]) 71 # Make sure we can iterate over it 72 chars = [chars] unless chars.respond_to?(:each) 73 # Run through them all and remove them 74 chars.each do | char | 75 legal_characters.delete(char) 76 end 77 end 78 79 upper_limit = legal_characters.size - 1 80 81 srand # seed rand 82 (1..self).collect do |num| 83 legal_characters[rand(upper_limit)] 84 end.join 85 end