rdice 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.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +48 -0
- data/Rakefile +1 -0
- data/lib/rdice.rb +3 -0
- data/lib/rdice/config.rb +19 -0
- data/lib/rdice/rdice.rb +84 -0
- data/lib/rdice/version.rb +3 -0
- data/rdice.gemspec +25 -0
- data/spec/rdice_spec.rb +30 -0
- data/spec/spec_helper.rb +1 -0
- metadata +86 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Dice
|
|
2
|
+
|
|
3
|
+
This gem was created with studies purpose. It simple create a dice and let you roll one or more dices with different faces.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add to your Gemfile and run the `bundle` command to install it.
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem "dice" # not published yet will not work
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
**Requires Ruby 1.9.3 or later.**
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
Them most basic use you can create a new instance of Dice and perform some rolls
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
dice = Dice.new
|
|
23
|
+
|
|
24
|
+
dice.roll
|
|
25
|
+
dice.roll_two
|
|
26
|
+
dice.roll_d20
|
|
27
|
+
dice.roll_three_d8
|
|
28
|
+
dice.roll_10_d3
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
This gem comes with shortcuts of one to twenty so you can call *.roll_four_d12*. If you want a dice with more than twenty sides you can explicit declare the number of sides with *.roll_32_d3*
|
|
32
|
+
|
|
33
|
+
If you call roll with only the number of times you want to roll it will roll the default dice with the required number of times
|
|
34
|
+
|
|
35
|
+
## Changing the defaults
|
|
36
|
+
|
|
37
|
+
When you call *.roll* method this roll a dice with the defaults faces and dices number (6 and 1). If you want to change the defaults you can in the class creation
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
dice = Dice.new(20,2)
|
|
41
|
+
|
|
42
|
+
dice.roll # will roll two d20 by default
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## License
|
|
46
|
+
|
|
47
|
+
Ruby on Rails is released under the MIT license:
|
|
48
|
+
[http://www.opensource.org/licenses/MIT](http://www.opensource.org/licenses/MIT)
|
data/Rakefile
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/rdice.rb
ADDED
data/lib/rdice/config.rb
ADDED
data/lib/rdice/rdice.rb
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
class Rdice
|
|
2
|
+
def initialize(faces=6, dices=1)
|
|
3
|
+
Rdice::Config.faces = faces
|
|
4
|
+
Rdice::Config.dices = dices
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def roll(faces=preferred_faces, dices=preferred_dices)
|
|
8
|
+
get_result(faces, dices)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def method_missing(method, *args, &block)
|
|
12
|
+
if method =~ /roll_d(\w+)/
|
|
13
|
+
roll($1.to_i)
|
|
14
|
+
elsif method =~ /roll_(\w+)_d(\w+)/
|
|
15
|
+
roll($2.to_i, translate_value($1))
|
|
16
|
+
elsif method =~ /roll_(\w+)/
|
|
17
|
+
roll(preferred_faces,translate_value($1))
|
|
18
|
+
else
|
|
19
|
+
super
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def preferred_faces
|
|
26
|
+
Rdice::Config.faces
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def preferred_dices
|
|
30
|
+
Rdice::Config.dices
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def get_result(faces, dices)
|
|
34
|
+
(1..dices).inject(0) {|a, b| a + rand(faces) + 1 }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def translate_value(number)
|
|
38
|
+
case number
|
|
39
|
+
when 'one'
|
|
40
|
+
value = 1
|
|
41
|
+
when 'two'
|
|
42
|
+
value = 2
|
|
43
|
+
when 'three'
|
|
44
|
+
value = 3
|
|
45
|
+
when 'four'
|
|
46
|
+
value = 4
|
|
47
|
+
when 'five'
|
|
48
|
+
value = 5
|
|
49
|
+
when 'six'
|
|
50
|
+
value = 6
|
|
51
|
+
when 'seven'
|
|
52
|
+
value = 7
|
|
53
|
+
when 'eight'
|
|
54
|
+
value = 8
|
|
55
|
+
when 'nine'
|
|
56
|
+
value = 9
|
|
57
|
+
when 'ten'
|
|
58
|
+
value = 10
|
|
59
|
+
when 'eleven'
|
|
60
|
+
value = 11
|
|
61
|
+
when 'twelve'
|
|
62
|
+
value = 12
|
|
63
|
+
when 'thirteen'
|
|
64
|
+
value = 13
|
|
65
|
+
when 'fourteen'
|
|
66
|
+
value = 14
|
|
67
|
+
when 'fifteen'
|
|
68
|
+
value = 15
|
|
69
|
+
when 'sixteen'
|
|
70
|
+
value = 16
|
|
71
|
+
when 'seventeen'
|
|
72
|
+
value = 17
|
|
73
|
+
when 'eighteen'
|
|
74
|
+
value = 18
|
|
75
|
+
when 'nineteen'
|
|
76
|
+
value = 19
|
|
77
|
+
when 'twenty'
|
|
78
|
+
value = 20
|
|
79
|
+
else
|
|
80
|
+
value = number.to_i
|
|
81
|
+
end
|
|
82
|
+
value
|
|
83
|
+
end
|
|
84
|
+
end
|
data/rdice.gemspec
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
|
3
|
+
require "rdice/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |s|
|
|
6
|
+
s.name = "rdice"
|
|
7
|
+
s.version = Rdice::VERSION
|
|
8
|
+
s.authors = ["Denis"]
|
|
9
|
+
s.email = ["de.tierno@gmail.com"]
|
|
10
|
+
s.homepage = ""
|
|
11
|
+
s.summary = %q{Roll dices with different faces}
|
|
12
|
+
s.description = %q{This gem allow you to set dices and roll it lots of times}
|
|
13
|
+
|
|
14
|
+
s.rubyforge_project = "rdice"
|
|
15
|
+
|
|
16
|
+
s.files = `git ls-files`.split("\n")
|
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
|
19
|
+
s.require_paths = ["lib"]
|
|
20
|
+
|
|
21
|
+
# specify any dependencies here; for example:
|
|
22
|
+
s.add_development_dependency "rspec"
|
|
23
|
+
s.add_development_dependency "rake"
|
|
24
|
+
# s.add_runtime_dependency "rest-client"
|
|
25
|
+
end
|
data/spec/rdice_spec.rb
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Rdice do
|
|
4
|
+
let(:dice) { Rdice.new }
|
|
5
|
+
|
|
6
|
+
describe "new" do
|
|
7
|
+
it "roll one dice" do
|
|
8
|
+
1.upto(6).include?(dice.roll).should be_true
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
describe "new with 2 dices" do
|
|
13
|
+
it "return value between 2 to 12" do
|
|
14
|
+
2.upto(12).include?(dice.roll_two).should be_true
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "cant return 1" do
|
|
18
|
+
30.times { 2.upto(12).include?(dice.roll_two).should_not == 1 }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "cant be lesser then 2" do
|
|
22
|
+
30.times { dice.roll_two.should_not < 2 }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "cant return 13" do
|
|
26
|
+
30.times { dice.roll_two.should_not > 13 }
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'rdice'
|
metadata
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rdice
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Denis
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-03-02 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: rspec
|
|
16
|
+
requirement: &70121011992320 !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '0'
|
|
22
|
+
type: :development
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: *70121011992320
|
|
25
|
+
- !ruby/object:Gem::Dependency
|
|
26
|
+
name: rake
|
|
27
|
+
requirement: &70121011991400 !ruby/object:Gem::Requirement
|
|
28
|
+
none: false
|
|
29
|
+
requirements:
|
|
30
|
+
- - ! '>='
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: *70121011991400
|
|
36
|
+
description: This gem allow you to set dices and roll it lots of times
|
|
37
|
+
email:
|
|
38
|
+
- de.tierno@gmail.com
|
|
39
|
+
executables: []
|
|
40
|
+
extensions: []
|
|
41
|
+
extra_rdoc_files: []
|
|
42
|
+
files:
|
|
43
|
+
- .gitignore
|
|
44
|
+
- Gemfile
|
|
45
|
+
- README.md
|
|
46
|
+
- Rakefile
|
|
47
|
+
- lib/rdice.rb
|
|
48
|
+
- lib/rdice/config.rb
|
|
49
|
+
- lib/rdice/rdice.rb
|
|
50
|
+
- lib/rdice/version.rb
|
|
51
|
+
- rdice.gemspec
|
|
52
|
+
- spec/rdice_spec.rb
|
|
53
|
+
- spec/spec_helper.rb
|
|
54
|
+
homepage: ''
|
|
55
|
+
licenses: []
|
|
56
|
+
post_install_message:
|
|
57
|
+
rdoc_options: []
|
|
58
|
+
require_paths:
|
|
59
|
+
- lib
|
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
61
|
+
none: false
|
|
62
|
+
requirements:
|
|
63
|
+
- - ! '>='
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
version: '0'
|
|
66
|
+
segments:
|
|
67
|
+
- 0
|
|
68
|
+
hash: -1608493834854290153
|
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
|
+
none: false
|
|
71
|
+
requirements:
|
|
72
|
+
- - ! '>='
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
segments:
|
|
76
|
+
- 0
|
|
77
|
+
hash: -1608493834854290153
|
|
78
|
+
requirements: []
|
|
79
|
+
rubyforge_project: rdice
|
|
80
|
+
rubygems_version: 1.8.10
|
|
81
|
+
signing_key:
|
|
82
|
+
specification_version: 3
|
|
83
|
+
summary: Roll dices with different faces
|
|
84
|
+
test_files:
|
|
85
|
+
- spec/rdice_spec.rb
|
|
86
|
+
- spec/spec_helper.rb
|