anybase 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.rdoc +5 -0
  2. data/VERSION +1 -1
  3. data/lib/anybase.rb +12 -1
  4. data/spec/to_spec.rb +17 -0
  5. metadata +2 -2
@@ -31,6 +31,11 @@ Anybase can also zeropad your output with whatever your "zero" character is.
31
31
  Anybase.new("012345678").to_native(1234, :zero_pad => 8)
32
32
  => '00001621'
33
33
 
34
+ Anybase also lets you create random numbers of any number of digits
35
+
36
+ Anybase.new("012345678").random(20)
37
+ => '62400274212676317317'
38
+
34
39
  Anybase gives you a few built-in:
35
40
  <tt>Anybase::Hex</tt>, <tt>Anybase::Base64</tt>, <tt>Anybase::Base64ForURL</tt> and <tt>Anybase::Base62</tt>
36
41
  (should all be pretty self-explanatory)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.7
1
+ 0.0.8
@@ -20,7 +20,18 @@ class Anybase
20
20
  def ignore_case?
21
21
  @ignore_case
22
22
  end
23
-
23
+
24
+ def random(digits, opts = nil)
25
+ zero_pad = opts && opts.key?(:zero_pad) ? opts[:zero_pad] : true
26
+ number = ''
27
+ digits.times { number << chars[rand(chars.size)]}
28
+ unless zero_pad
29
+ number.sub!(/\A#{Regexp.quote(chars[0].chr)}+/, '')
30
+ number = chars[0].chr if number.empty?
31
+ end
32
+ number
33
+ end
34
+
24
35
  def to_i(val)
25
36
  num = 0
26
37
  (0...val.size).each{|i|
@@ -16,4 +16,21 @@ describe Anybase, "to" do
16
16
  result.should == '00001621'
17
17
  end
18
18
 
19
+ it "create random numbers" do
20
+ srand(10)
21
+ result = Anybase.new("012345678").random(10)
22
+ result.should == '4010180864'
23
+
24
+ srand(39)
25
+ Anybase.new("012345678").random(10, :zero_pad => false).should == '463048140'
26
+ end
27
+
28
+ it "return a zero if thats all it can for a random number" do
29
+ result = Anybase.new("0").random(10)
30
+ result.should == '0000000000'
31
+
32
+ result = Anybase.new("0").random(10, :zero_pad => false)
33
+ result.should == '0'
34
+ end
35
+
19
36
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anybase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Hull
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-01 00:00:00 -05:00
12
+ date: 2010-03-15 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15