lib/rujitsu/fixnum.rb in brightbox-rujitsu-0.1.8 vs lib/rujitsu/fixnum.rb in brightbox-rujitsu-0.1.9
- old
+ new
@@ -18,17 +18,20 @@
# 5.random_numbers(:from => 1, :to => 5)
def random_numbers( opts = {} )
# Then set some defaults, just in case
upper = opts[:to] || 9
lower = opts[:from] || 0
+ only = opts[:only] || :both
# And finally calculate the number
n = []
- self.times do
+ (self - 1).times do
i = (lower..upper).to_a.sort_by { rand }.first
n << i.to_s
end
+ # add the last digit according to :only
+ n << end_number_choices(only).select {|x| (lower <= x) && (x <= upper) }.sort_by { rand }.first.to_s
n.join("")
end
# produce a string of N random characters
# 5.random_characters
def random_characters
@@ -40,14 +43,26 @@
VOWELS = ['a', 'e', 'i', 'o', 'u']
LETTERS = ('a'..'z').to_a
CONSONANTS = LETTERS - VOWELS
NUMBERS = ('0'..'9').to_a
CHARACTERS = LETTERS + NUMBERS
+ EVENS = %w(0 2 4 6 8).map {|x| x.to_i }
+ ODDS = %w(1 3 5 7 9).map {|x| x.to_i }
def generate_random_string_using(legal_characters)
upper_limit = legal_characters.size - 1
(1..self).collect do |num|
legal_characters[rand(upper_limit)]
end.join
end
+
+ def end_number_choices opt
+ case opt
+ when :even
+ EVENS
+ when :odd
+ ODDS
+ else
+ EVENS | ODDS
+ end
+ end
end
-