randy 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +8 -0
- data/LICENSE +22 -0
- data/README.md +54 -0
- data/Rakefile +5 -0
- data/lib/randy.rb +17 -0
- data/lib/randy/version.rb +3 -0
- data/randy.gemspec +16 -0
- data/spec/randy_spec.rb +47 -0
- data/spec/spec_helper.rb +3 -0
- metadata +78 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Dan Tao
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Randy
|
2
|
+
|
3
|
+
A little library for easily producing random data using Ruby's built-in `rand` function.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'randy'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install randy
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Currently **randy** can produce two kinds of random data (in theory this will change).
|
22
|
+
|
23
|
+
### Strings
|
24
|
+
|
25
|
+
The original main purpose of this library is to produce random strings easily.
|
26
|
+
|
27
|
+
For example, to produce a random 10-character string:
|
28
|
+
|
29
|
+
# Example output: BASWCEV3TQ
|
30
|
+
Randy.string(10)
|
31
|
+
|
32
|
+
By default, the result string will comprise characters from the digits 0-9 and the English alphabet.
|
33
|
+
|
34
|
+
This can be changed by specifying the characters to be used:
|
35
|
+
|
36
|
+
# Example output: baaabbabac
|
37
|
+
Randy.string(10, "abc")
|
38
|
+
|
39
|
+
### Numbers
|
40
|
+
|
41
|
+
It's already easy to produce random numbers using Ruby. However, **randy** makes it *even easier*.
|
42
|
+
|
43
|
+
To produce a random *integer*, just specify a range:
|
44
|
+
|
45
|
+
# Will include some number between 1 and 10
|
46
|
+
Randy.integer(1..10)
|
47
|
+
|
48
|
+
The result will be consistent with how Ruby ranges work; that is, an inclusive range (`..`) will potentially include the upper bound while an exclusive range (`...`) will not.
|
49
|
+
|
50
|
+
Admittedly, the above is quite trivial to implement by hand, e.g., with `1 + rand(10)`. Think of it simply as a nicer interface to `rand`.
|
51
|
+
|
52
|
+
The same functionality is also exposed to decimal values:
|
53
|
+
|
54
|
+
Randy.decimal((1.4)..(6.2))
|
data/Rakefile
ADDED
data/lib/randy.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
module Randy
|
2
|
+
DEFAULT_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("").freeze
|
3
|
+
|
4
|
+
def self.string(length, chars=DEFAULT_CHARS)
|
5
|
+
chars = chars.is_a?(String) ? chars.split("") : chars
|
6
|
+
(1..length).inject("") { |s, c| s << chars[rand(chars.count)] }
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.integer(range)
|
10
|
+
return range.min if range.max == range.min
|
11
|
+
return range.min + rand(1 + range.max - range.min)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.decimal(range)
|
15
|
+
return range.begin + ((range.end - range.begin) * rand())
|
16
|
+
end
|
17
|
+
end
|
data/randy.gemspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path("../lib/randy/version", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Dan Tao"]
|
6
|
+
gem.email = ["daniel.tao@gmail.com"]
|
7
|
+
gem.description = %q{Randy makes it that much easier to generate random stuff.}
|
8
|
+
gem.summary = %q{A little library for easily producing random data using Ruby's built-in rand function.}
|
9
|
+
gem.homepage = "http://dtao.github.com/randy/"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.test_files = gem.files.grep(%r{^spec/})
|
13
|
+
gem.name = "randy"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = Randy::VERSION
|
16
|
+
end
|
data/spec/randy_spec.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "spec_helper")
|
2
|
+
|
3
|
+
require "randy"
|
4
|
+
|
5
|
+
describe Randy do
|
6
|
+
describe "::string" do
|
7
|
+
it "produces a string of the given length" do
|
8
|
+
Randy.string(5).length.should == 5
|
9
|
+
end
|
10
|
+
|
11
|
+
it "produces an empty string given a length of zero" do
|
12
|
+
Randy.string(0).length.should == 0
|
13
|
+
end
|
14
|
+
|
15
|
+
it "produces an empty string given a negative length" do
|
16
|
+
Randy.string(-1).length.should == 0
|
17
|
+
end
|
18
|
+
|
19
|
+
it "includes only the specified characters in the result string" do
|
20
|
+
Randy.string(100, ["a", "b", "c"]).should =~ /^[abc]{100}$/
|
21
|
+
end
|
22
|
+
|
23
|
+
it "will also accept a set of characters as a string" do
|
24
|
+
Randy.string(100, "abc").should =~ /^[abc]{100}$/
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "::integer" do
|
29
|
+
it "produces the only possible value for a range of size 1" do
|
30
|
+
Randy.integer(42..42).should == 42
|
31
|
+
end
|
32
|
+
|
33
|
+
it "produces a value within the given range" do
|
34
|
+
1000.times do
|
35
|
+
(1..10).should include(Randy.integer(1..10))
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it "might potentially produce the max of an inclusive range" do
|
40
|
+
1000.times.map { Randy.integer(1..5) }.should include(5)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "will not produce the max of an exclusive range" do
|
44
|
+
1000.times.map { Randy.integer(1...5) }.should_not include(5)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: randy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Dan Tao
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-07-04 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Randy makes it that much easier to generate random stuff.
|
23
|
+
email:
|
24
|
+
- daniel.tao@gmail.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- lib/randy.rb
|
38
|
+
- lib/randy/version.rb
|
39
|
+
- randy.gemspec
|
40
|
+
- spec/randy_spec.rb
|
41
|
+
- spec/spec_helper.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://dtao.github.com/randy/
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.4.2
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: A little library for easily producing random data using Ruby's built-in rand function.
|
76
|
+
test_files:
|
77
|
+
- spec/randy_spec.rb
|
78
|
+
- spec/spec_helper.rb
|