randint 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ # -*- ruby -*-
2
+
3
+ require "autotest/restart"
4
+
5
+ # Autotest.add_hook :initialize do |at|
6
+ # at.testlib = "minitest/unit"
7
+ #
8
+ # at.extra_files << "../some/external/dependency.rb"
9
+ #
10
+ # at.libs << ":../some/external"
11
+ #
12
+ # at.add_exception "vendor"
13
+ #
14
+ # at.add_mapping(/dependency.rb/) do |f, _|
15
+ # at.files_matching(/test_.*rb$/)
16
+ # end
17
+ #
18
+ # %w(TestA TestB).each do |klass|
19
+ # at.extra_class_map[klass] = "test/test_misc.rb"
20
+ # end
21
+ # end
22
+
23
+ # Autotest.add_hook :run_command do |at|
24
+ # system "rake build"
25
+ # end
File without changes
@@ -0,0 +1,4 @@
1
+ === 0.0.1 / 2014-02-14
2
+
3
+ * 1 major enhancement
4
+ * Birthday!
@@ -0,0 +1,10 @@
1
+ .autotest
2
+ History.md
3
+ Manifest.txt
4
+ README.md
5
+ Rakefile
6
+ bin/randint
7
+ lib/randint.rb
8
+ lib/randint/error.rb
9
+ randint.gemspec
10
+ test/test_randint.rb
@@ -0,0 +1,52 @@
1
+ # Randint
2
+
3
+ * https://github.com/awesome/randint
4
+
5
+ ## DESCRIPTION:
6
+
7
+ Creates positive integer with specified number of digits (i.e., specified "length" when coverted to string)
8
+
9
+ ## FEATURES:
10
+
11
+ * Specify random number generator: Kernel (default), Random or SecureRandom
12
+ * Kernel#rand and Random#rand are essentially the same and both included for ocd
13
+
14
+ ## REQUIREMENTS:
15
+
16
+ * Ruby >= 1.9.3
17
+
18
+ ## INSTALL:
19
+
20
+ $ gem install 'randint'
21
+
22
+ ## USAGE:
23
+
24
+ Randint.create(3)
25
+ #=> 111
26
+ Randint.create(50, SecureRandom)
27
+ #=> 45777499250739151797487680834117802518338464049274
28
+
29
+ ## LICENSE:
30
+
31
+ (The MIT License)
32
+
33
+ Copyright (c) 2014 SoAwesomeMan
34
+
35
+ Permission is hereby granted, free of charge, to any person obtaining
36
+ a copy of this software and associated documentation files (the
37
+ 'Software'), to deal in the Software without restriction, including
38
+ without limitation the rights to use, copy, modify, merge, publish,
39
+ distribute, sublicense, and/or sell copies of the Software, and to
40
+ permit persons to whom the Software is furnished to do so, subject to
41
+ the following conditions:
42
+
43
+ The above copyright notice and this permission notice shall be
44
+ included in all copies or substantial portions of the Software.
45
+
46
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
47
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
48
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
49
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
50
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
51
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
52
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,22 @@
1
+ # -*- ruby -*-
2
+
3
+ require "rubygems"
4
+ require "hoe"
5
+
6
+ Hoe.plugin :clean
7
+ Hoe.plugin :yard
8
+ Hoe.plugin :gemspec
9
+
10
+ Hoe.spec "randint" do
11
+ developer("SoAwesomeMan", "callme@1800AWESO.ME")
12
+
13
+ self.yard_title = 'Randint (0.0.2)'
14
+ self.yard_markup = 'markdown'
15
+ self.yard_opts = []
16
+
17
+ clean_globs << '.yardoc'
18
+
19
+ license "MIT"
20
+ end
21
+
22
+ # vim: syntax=ruby
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ abort "you need to write me"
@@ -0,0 +1,90 @@
1
+ require "securerandom"
2
+ require "randint/error"
3
+
4
+ module Randint
5
+ VERSION = "0.0.2"
6
+
7
+ extend self
8
+
9
+ # Creates positive integer with specified number of digits (i.e., specified "length" when coverted to string)
10
+ #
11
+ # @param length [Integer]
12
+ # @param lib [Class,Module] the lib to generate component random numbers from, `Kernel` or `Random` or `SecureRandom`
13
+ # @return [Integer]
14
+ def create(length, lib=nil)
15
+ raise Randint::Error::InvalidArg unless length.is_a?(Integer) and length > 0
16
+ raise Randint::Error::InvalidLib if lib and ![::Kernel,::Random,::SecureRandom].include?(lib)
17
+ lib ||= Kernel
18
+ n = ''
19
+ n << rand_string(lib) until n.length >= length
20
+ n[0..length-1].to_i
21
+ end
22
+
23
+ # format lib's rand string
24
+ #
25
+ # @param lib [Class,Module]
26
+ # @return [String]
27
+ def rand_string(lib)
28
+ x = send("#{underscore(lib.to_s)}_rand")
29
+ format_left(x.to_s)
30
+ end
31
+
32
+ # converts CamelCase to underscore
33
+ #
34
+ # @author https://gist.github.com/timcharper/4027440
35
+ # @param string [String]
36
+ # @return [String]
37
+ def underscore(string)
38
+ string.gsub(/(.)([A-Z])/,'\1_\2').downcase
39
+ end
40
+
41
+ # remove leading zeros and decimal point from left of string
42
+ #
43
+ # @param string [String] Float#to_s
44
+ # @return [String]
45
+ def format_left(string)
46
+ string.gsub(/^[0\.]*/,'')
47
+ end
48
+
49
+ # Counts digits in fractional part of decimal number
50
+ #
51
+ # @author http://stackoverflow.com/a/8597808/1076207
52
+ # @param float [Float]
53
+ # @return [Integer]
54
+ def decimal_place_length(float)
55
+ num = 0
56
+ while(float != float.to_i)
57
+ num += 1
58
+ float *= 10
59
+ end
60
+ num
61
+ end
62
+
63
+ # Creates random number via SecureRandom#random_number
64
+ #
65
+ # @see http://rubydoc.info/stdlib/securerandom/1.9.3/SecureRandom.random_number
66
+ # @return [String]
67
+ def secure_random_rand
68
+ SecureRandom.random_number
69
+ end
70
+
71
+ # Creates random number via Random#rand
72
+ #
73
+ # @return [String]
74
+ # @see http://ruby-doc.org/core-1.9.3/Random.html#method-c-rand
75
+ def random_rand
76
+ Random.rand
77
+ end
78
+
79
+ # Creates random number via Kernel#rand
80
+ #
81
+ # @return [String]
82
+ # @see http://ruby-doc.org/core-1.9.3/Kernel.html#method-i-rand
83
+ #
84
+ # @notes
85
+ # "The important difference to note is that if you just call rand() you are calling Kernel#rand, which only supports a max argument. If you want to pass a range, you have to use Random#rand, meaning you have to implement this way."
86
+ # http://stackoverflow.com/questions/4395095/how-to-generate-a-random-number-between-a-and-b-in-ruby#comment19986191_13119700
87
+ def kernel_rand
88
+ Kernel.rand
89
+ end
90
+ end
@@ -0,0 +1,6 @@
1
+ module Randint
2
+ module Error
3
+ class InvalidArg < StandardError; end
4
+ class InvalidLib < StandardError; end
5
+ end
6
+ end
@@ -0,0 +1,38 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "randint"
5
+ s.version = "0.0.2.20140218094847"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["SoAwesomeMan"]
9
+ s.date = "2014-02-18"
10
+ s.description = "Creates positive integer with specified number of digits (i.e., specified \"length\" when coverted to string)"
11
+ s.email = ["callme@1800AWESO.ME"]
12
+ s.executables = ["randint"]
13
+ s.extra_rdoc_files = ["History.md", "Manifest.txt", "README.md", "History.md"]
14
+ s.files = [".autotest", "History.md", "Manifest.txt", "README.md", "Rakefile", "bin/randint", "lib/randint.rb", "lib/randint/error.rb", "randint.gemspec", "test/test_randint.rb", ".gemtest"]
15
+ s.homepage = "https://github.com/awesome/randint"
16
+ s.licenses = ["MIT"]
17
+ s.rdoc_options = ["--title", "Randint (0.0.2)", "--markup", "markdown", "--quiet"]
18
+ s.require_paths = ["lib"]
19
+ s.rubyforge_project = "randint"
20
+ s.rubygems_version = "1.8.23"
21
+ s.summary = "Creates positive integer with specified number of digits (i.e., specified \"length\" when coverted to string)"
22
+ s.test_files = ["test/test_randint.rb"]
23
+
24
+ if s.respond_to? :specification_version then
25
+ s.specification_version = 3
26
+
27
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
28
+ s.add_development_dependency(%q<hoe-yard>, [">= 0.1.2"])
29
+ s.add_development_dependency(%q<hoe>, ["~> 3.9"])
30
+ else
31
+ s.add_dependency(%q<hoe-yard>, [">= 0.1.2"])
32
+ s.add_dependency(%q<hoe>, ["~> 3.9"])
33
+ end
34
+ else
35
+ s.add_dependency(%q<hoe-yard>, [">= 0.1.2"])
36
+ s.add_dependency(%q<hoe>, ["~> 3.9"])
37
+ end
38
+ end
@@ -0,0 +1,43 @@
1
+ require "minitest/autorun"
2
+ require "randint"
3
+
4
+ class TestRandint < MiniTest::Unit::TestCase
5
+ def setup
6
+ @libs = [nil,Kernel,Random,SecureRandom]
7
+ end
8
+
9
+ def test_create_double_digets
10
+ @libs.each do |lib|
11
+ length = rand(10..100)
12
+ act = Randint.create(length, lib)
13
+ assert_kind_of Integer, act
14
+ assert_equal(length, act.to_s.length)
15
+ end
16
+ end
17
+
18
+ def test_create_single_digit
19
+ @libs.each do |lib|
20
+ length = rand(1..9)
21
+ act = Randint.create(length, lib)
22
+ assert_kind_of Integer, act
23
+ assert_equal(length, act.to_s.length)
24
+ end
25
+ end
26
+
27
+ def test_int_zero
28
+ @libs.each do |lib|
29
+ assert_raises(Randint::Error::InvalidArg) {Randint.create(0, lib)}
30
+ end
31
+ end
32
+
33
+ def test_format_left
34
+ exp = rand(1..1000).to_s
35
+ #array = ("." * rand(1..3)) + ("0" * rand(1..3)).chars.to_a.permutation.map {|x| x.join + exp.to_s}
36
+ arr = "#{"." * rand(1..3)}#{"0" * rand(1..3)}".chars.to_a.permutation.map(&:join).map {|x| "#{x}#{exp}"}
37
+ arr.each do |x|
38
+ act = Randint.format_left(x)
39
+ assert_instance_of String, act
40
+ assert_equal exp, act
41
+ end
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: randint
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - SoAwesomeMan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hoe-yard
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.1.2
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.1.2
30
+ - !ruby/object:Gem::Dependency
31
+ name: hoe
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3.9'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.9'
46
+ description: Creates positive integer with specified number of digits (i.e., specified
47
+ "length" when coverted to string)
48
+ email:
49
+ - callme@1800AWESO.ME
50
+ executables:
51
+ - randint
52
+ extensions: []
53
+ extra_rdoc_files:
54
+ - History.md
55
+ - Manifest.txt
56
+ - README.md
57
+ files:
58
+ - .autotest
59
+ - History.md
60
+ - Manifest.txt
61
+ - README.md
62
+ - Rakefile
63
+ - bin/randint
64
+ - lib/randint.rb
65
+ - lib/randint/error.rb
66
+ - randint.gemspec
67
+ - test/test_randint.rb
68
+ - .gemtest
69
+ homepage: https://github.com/awesome/randint
70
+ licenses:
71
+ - MIT
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --title
75
+ - Randint (0.0.2)
76
+ - --markup
77
+ - markdown
78
+ - --quiet
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project: randint
95
+ rubygems_version: 1.8.23
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Creates positive integer with specified number of digits (i.e., specified
99
+ "length" when coverted to string)
100
+ test_files:
101
+ - test/test_randint.rb