random_in 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ Zjk5N2EwNWZlMWRiMDU5MTQ1MzM0ZTgxMmVlODIzNTg1MWJiNjYzZg==
5
+ data.tar.gz: !binary |-
6
+ YzI5N2M3YWI3ODg0ZGYxMjE0NGIzNzY0Yjc1Y2I2ZjQ0YmIzZDdiOQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MjE1OWY1YjI5ODAwYWQ5Mjg3MWE4YTMxM2MyNmJkNTI5Yjk4OTAyOWY2ZjE3
10
+ Mzk4ZGY0YzA0ZDkxOWQ4Y2FkYjg4NmJlN2M1MGQ5NGJkZmI2ZDA4YzUwN2Rh
11
+ YWZjNjBkZmJjNzA3OWUyMjM0ZjAyMTlkNjk0NmNkYWZlOThiNmQ=
12
+ data.tar.gz: !binary |-
13
+ MjQ4Y2FkYjRiYTM0MWI1ODA3Y2FiNDk4MmJiOTJiMDY1MjdiYjVhMWU3MjA0
14
+ ZTU1Y2I5Njc5MDA0NWJhYTE4ZTRhMjkzNzJkN2U5NDg5ZjNiZjkzYjJiOGEw
15
+ MjdlMGZmNzQ4MzgzMzgwMzVkZDdjN2RlZGNkMGE1YWEyMDcyODU=
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Green
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ = RandomIn
2
+
3
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler'
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+ begin
9
+ require 'rdoc/task'
10
+ rescue LoadError
11
+ require 'rdoc/rdoc'
12
+ require 'rake/rdoctask'
13
+ RDoc::Task = Rake::RDocTask
14
+ end
15
+
16
+ RDoc::Task.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'RandomIn'
19
+ rdoc.options << '--line-numbers'
20
+ rdoc.rdoc_files.include('README.rdoc')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rake/testtask'
29
+
30
+ Rake::TestTask.new(:test) do |t|
31
+ t.libs << 'lib'
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/*_test.rb'
34
+ t.verbose = false
35
+ end
36
+
37
+ desc "Run tests"
38
+
39
+ task :default => :test
@@ -0,0 +1,6 @@
1
+ # coding: utf-8
2
+
3
+ require "random_in/core"
4
+
5
+ module RandomIn
6
+ end
@@ -0,0 +1,55 @@
1
+ # coding: utf-8
2
+
3
+ Integer.class_eval do
4
+ def method_missing(name, from = 0, to = 10, may_repeat = false, &block)
5
+ if(name.eql?(:random_in) || name.eql?(:randoms_in))
6
+
7
+ from = from.to_i
8
+ to = to.to_i
9
+
10
+ #
11
+ # To avoid infinite loop.
12
+ # For negative "from" a zero appears:
13
+ # 3.randoms_in(-1, 1, false) <-- is ok sinse we have 0, [-1, 0, 1]
14
+ #
15
+ if(!may_repeat)
16
+ msg = "The result set is going to be less than your number. When parameter \"may_repeat\" is false it is an error."
17
+ case true
18
+ when(from < 0 && ((-(from) + to) < self.to_i - 1)), (from > 0 && (self.to_i > to.to_i))
19
+ raise(ArgumentError, msg, caller)
20
+ end
21
+ end
22
+
23
+
24
+ # "from" has to be less than "to"
25
+ if(from > to)
26
+ msg = "Parameter \"to\" has to be more than parameter \"from\"."
27
+ raise(ArgumentError, msg, caller)
28
+ end
29
+
30
+
31
+ result = []
32
+ self.times { |i|
33
+ n = rand(from..to)
34
+ if(!may_repeat)
35
+ while(result.index(n) != nil)
36
+ n = rand(from..to)
37
+ end
38
+ end
39
+ result << n
40
+ }
41
+
42
+ # Если передается блок, то в него возвращается и резульат и
43
+ # само число:
44
+ # 5.randoms_within(1, 10) { |n, rs| puts "i = #{n}; n = #{rs}" }
45
+ # #=> n = 5; rs = [4, 2, 10, 7, 8]
46
+ if block_given?
47
+ yield self, result
48
+ else
49
+ return result
50
+ end
51
+ else
52
+ super
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ # coding: utf-8
2
+
3
+ module RandomIn
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,45 @@
1
+ # coding: utf-8
2
+
3
+ require "test/unit"
4
+ require "random_in"
5
+
6
+ class RandomInTest < Test::Unit::TestCase
7
+
8
+ # Should throw exception if n is equals to or more than "to"
9
+ # when "may_repeat" param is false
10
+ def test_throws_exeption
11
+ # n is equals to "to"
12
+ assert_raise(ArgumentError) do
13
+ 10.random_in(1,10,false)
14
+ end
15
+
16
+ # n is more than "to"
17
+ assert_raise(ArgumentError) do
18
+ 10.random_in(1,9,false)
19
+ end
20
+
21
+ # "to" has to be more than "from"
22
+ assert_raise(ArgumentError) do
23
+ 10.random_in(9,1,false)
24
+ end
25
+ end
26
+
27
+ # the return is correct
28
+ # the resul set doesn't contain repeat values
29
+ def test_return
30
+ rs = 5.random_in(1,6,false)
31
+ assert_equal(5, rs.length)
32
+ assert_equal(Array, rs.class)
33
+ end
34
+
35
+ # Repeat and not repeat values in result set
36
+ def test_repeat_and_no_repeat_values
37
+ # no repeat values in result set
38
+ rs1 = 15.random_in(1,16,false)
39
+ assert_nil(rs1.uniq!)
40
+
41
+ # repeat values exists in rs
42
+ rs2 = 6.random_in(1,5,true)
43
+ assert_operator(rs2.uniq!.length, :<=, rs2.length)
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: random_in
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Wzup
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A generator of random numbers. Implemented as a built-in Integer class'
14
+ method. Generates an array of specified quantity of random numbers. Generated numbers
15
+ may repeat or may not repeat - it is configurable. You can set a range of numbers
16
+ from which to generate by defining the beggining and the end of the range. Ruby's
17
+ Random class is used as generator. Tested.
18
+ email:
19
+ - londonein@gmain.com
20
+ executables: []
21
+ extensions: []
22
+ extra_rdoc_files: []
23
+ files:
24
+ - lib/random_in/core.rb
25
+ - lib/random_in/version.rb
26
+ - lib/random_in.rb
27
+ - MIT-LICENSE
28
+ - Rakefile
29
+ - README.rdoc
30
+ - test/random_in_test.rb
31
+ homepage: https://github.com/wzup/random_in
32
+ licenses: []
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.0.3
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: A generator of a set of random numbers. Implemented as a built-in Integer
54
+ class' method.
55
+ test_files:
56
+ - test/random_in_test.rb