rudil 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2009-10-11
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,15 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/rudil.rb
6
+ lib/rudil/die.rb
7
+ lib/rudil/throw.rb
8
+ script/console
9
+ script/destroy
10
+ script/generate
11
+ spec/die_spec.rb
12
+ spec/spec.opts
13
+ spec/spec_helper.rb
14
+ spec/throw_spec.rb
15
+ tasks/rspec.rake
data/README.rdoc ADDED
@@ -0,0 +1,80 @@
1
+ = rudil
2
+
3
+ * http://github.com/sMAshdot/rudil
4
+
5
+ == DESCRIPTION:
6
+
7
+ rudil is a dice library for ruby.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ Roll dice with any number of sides.
12
+ Methods for single and multiple rolls.
13
+ Retrieve useful about the rolls easily.
14
+
15
+ == SYNOPSIS:
16
+
17
+ === simple rolling
18
+
19
+ require 'rudil'
20
+ die = Rudil::Die.new
21
+ or
22
+ require 'rudil'
23
+ include Rudil
24
+ die = Die.new
25
+ creates a 6-sided die per default
26
+ but you could also use Die.new(n) to
27
+ obtain a die with more sides.
28
+
29
+ die.roll
30
+ => 3
31
+ returns the result of a single roll
32
+
33
+ die.throw(5)
34
+ => [6, 5, 3, 1, 3]
35
+ returns results of any number of rolls as array
36
+
37
+ === analysing the results
38
+
39
+ start with a die
40
+ die = Die.new(20)
41
+
42
+ collect the rolls in a throw
43
+ throw = Throw.new( die.throw(5) )
44
+
45
+ Throw is a subclass of array with a few convenience methods added.
46
+ You can get information about
47
+ - the lowest roll
48
+ - the highest roll
49
+ - the mean
50
+ - the number of rolls above a certain value (or equal and above)
51
+ - the number of rolls below a certain value (or equal and below) FIX (code sample of usage)
52
+
53
+ == INSTALL:
54
+
55
+ soon
56
+
57
+ == LICENSE:
58
+
59
+ (The MIT License)
60
+
61
+ Copyright (c) 2009 FIXME full name
62
+
63
+ Permission is hereby granted, free of charge, to any person obtaining
64
+ a copy of this software and associated documentation files (the
65
+ 'Software'), to deal in the Software without restriction, including
66
+ without limitation the rights to use, copy, modify, merge, publish,
67
+ distribute, sublicense, and/or sell copies of the Software, and to
68
+ permit persons to whom the Software is furnished to do so, subject to
69
+ the following conditions:
70
+
71
+ The above copyright notice and this permission notice shall be
72
+ included in all copies or substantial portions of the Software.
73
+
74
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
75
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
76
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
77
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
78
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
79
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
80
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/rudil'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'rudil' do
14
+ self.developer 'Marc-Antonio Bisotti', 'mail@marc-antonio.de'
15
+ self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
16
+ self.rubyforge_name = self.name # TODO this is default value
17
+ # self.extra_deps = [['activesupport','>= 2.0.2']]
18
+
19
+ end
20
+
21
+ require 'newgem/tasks'
22
+ Dir['tasks/**/*.rake'].each { |t| load t }
23
+
24
+ # TODO - want other tests/tasks run by default? Add them to the list
25
+ # remove_task :default
26
+ # task :default => [:spec, :features]
data/lib/rudil/die.rb ADDED
@@ -0,0 +1,31 @@
1
+ module Rudil
2
+ class Die
3
+ def initialize sides=6
4
+ unless sides >= 1 and sides <= 120
5
+ raise ArgumentError
6
+ # "120 sides should be enough for anybody."
7
+ # - sMAshdot
8
+ # Such a die would be an Hexakis Icosahedron, and I challenge
9
+ # anyone to build it.
10
+ # The biggest manufactured die to date has 100 sides and is
11
+ # trademarked as Zocchihedron. It isn't a polyhedron anymore
12
+ # but rather a ball with flattened planes (think golfball).
13
+ else
14
+ @sides = sides
15
+ end
16
+ end
17
+
18
+ def throw times=1
19
+ if times <= 0
20
+ raise ArgumentError
21
+ end
22
+ Array.new(times).fill { roll }
23
+ end
24
+
25
+ def roll
26
+ # the part where randomness happens has it's own method
27
+ # to simplify unit-testing by permitting it to be stubbed.
28
+ rand(@sides)+1
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,38 @@
1
+ module Rudil
2
+ class Throw < Array
3
+ def to_s
4
+ inspect
5
+ end
6
+
7
+ def lowest
8
+ sort.first
9
+ end
10
+
11
+ def highest
12
+ sort.last
13
+ end
14
+
15
+ def mean
16
+ if size < 1
17
+ return nil
18
+ end
19
+ inject(0.0) { |sum, i| sum + i } / size
20
+ end
21
+
22
+ def above m
23
+ select { |n| n > m }.size
24
+ end
25
+
26
+ def eq_or_above m
27
+ select { |n| n >= m }.size
28
+ end
29
+
30
+ def below m
31
+ select { |n| n < m }.size
32
+ end
33
+
34
+ def eq_or_below m
35
+ select { |n| n <= m }.size
36
+ end
37
+ end
38
+ end
data/lib/rudil.rb ADDED
@@ -0,0 +1,11 @@
1
+ libdir = File.dirname(__FILE__)
2
+
3
+ $LOAD_PATH.unshift(libdir) unless
4
+ $LOAD_PATH.include?(libdir) || $LOAD_PATH.include?(File.expand_path(libdir))
5
+
6
+ module Rudil
7
+ VERSION = '0.0.1'
8
+ end
9
+
10
+ require 'rudil/die'
11
+ require 'rudil/throw'
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/rudil.rb'}"
9
+ puts "Loading rudil gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
data/spec/die_spec.rb ADDED
@@ -0,0 +1,73 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ # classes dealing with random numbers are not easily unit-tested.
4
+ # this number represents the number of dice-throwing to do until
5
+ # the Die is considered "tested enough"
6
+ NB_OF_ROLLS = 180
7
+ # 180 throws correspond to a 1-(19/20)^180=0.99990 chance of
8
+ # throwing a certain number at least once on a twenty sided die
9
+
10
+ if NB_OF_ROLLS < 1
11
+ raise ScriptError
12
+ end
13
+
14
+ describe Die do
15
+ it "should not want to have less than 1 or more than 120 sides" do
16
+ lambda { Die.new(0) }.should raise_error(ArgumentError)
17
+ lambda { Die.new(121) }.should raise_error(ArgumentError)
18
+ end
19
+
20
+ it "should roll an integer" do
21
+ die = Die.new
22
+ die.roll.class.should == Fixnum
23
+ end
24
+
25
+ it "should not roll a number smaller than one" do
26
+ die = Die.new
27
+ collection = Array.new(NB_OF_ROLLS).fill { die.roll }
28
+ collection.sort[0].should == 1
29
+ end
30
+
31
+ it "should not roll a number bigger than 6 per default" do
32
+ die = Die.new
33
+ collection = Array.new(NB_OF_ROLLS).fill { die.roll }
34
+ collection.sort[NB_OF_ROLLS - 1].should == 6
35
+ end
36
+
37
+ it "should not roll a number bigger than the number of sides" do
38
+ die = Die.new(20)
39
+ collection = Array.new(NB_OF_ROLLS).fill { die.roll }
40
+ collection.sort[NB_OF_ROLLS - 1].should == 20
41
+ end
42
+
43
+ it "should be thrown once per default" do
44
+ die = Die.new
45
+ die.throw.length.should == 1
46
+ end
47
+
48
+ it "should be thrown as often as told" do
49
+ die = Die.new
50
+ die.throw(10).length.should == 10
51
+ end
52
+
53
+ it "should not want to be thrown less than once" do
54
+ die = Die.new
55
+ lambda { die.throw(-1) }.should raise_error(ArgumentError)
56
+ lambda { die.throw(0) }.should raise_error(ArgumentError)
57
+ end
58
+
59
+ it "should return the result after it has been thrown once" do
60
+ die = Die.new()
61
+ die.stub!(:roll).and_return(6)
62
+ throw = die.throw(1)
63
+ throw[0].should == 6
64
+ end
65
+
66
+ it "should return the result after it has been thrown any number of times" do
67
+ die = Die.new()
68
+ die.stub!(:roll).and_return(1,2,3,4,5,6)
69
+ throw = die.throw(6)
70
+ throw[0].should == 1
71
+ throw[5].should == 6
72
+ end
73
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,13 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ libdir = File.dirname(File.dirname(__FILE__)) + '/lib'
10
+ $LOAD_PATH.unshift libdir unless $LOAD_PATH.include?(libdir)
11
+
12
+ require 'rudil'
13
+ include Rudil
@@ -0,0 +1,55 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Throw do
4
+ it "should be pretty when casted into string" do
5
+ throw = Throw.new [1,2,3,4,5,6]
6
+ throw.to_s.should == "[1, 2, 3, 4, 5, 6]"
7
+ end
8
+
9
+ it "should return its lowest roll" do
10
+ throw = Throw.new []
11
+ throw.lowest.should == nil
12
+ throw = Throw.new [3]
13
+ throw.lowest.should == 3
14
+ throw = Throw.new [2,5,1,6]
15
+ throw.lowest.should == 1
16
+ end
17
+
18
+ it "should return its highest roll" do
19
+ throw = Throw.new []
20
+ throw.highest.should == nil
21
+ throw = Throw.new [3]
22
+ throw.highest.should == 3
23
+ throw = Throw.new [2,5,1,6]
24
+ throw.highest.should == 6
25
+ end
26
+
27
+ it "should return its mean" do
28
+ throw = Throw.new []
29
+ throw.mean.should == nil
30
+ throw = Throw.new [1,2,3]
31
+ throw.mean.should == 2.0
32
+ throw = Throw.new [1,2,3,4]
33
+ throw.mean.should == 2.5
34
+ end
35
+
36
+ it "should return number of rolls above a certain value" do
37
+ throw = Throw.new [1,2,3,4,5,6]
38
+ throw.above(3).should == 3
39
+ end
40
+
41
+ it "should return number of rolls equal or below a value" do
42
+ throw = Throw.new [1,2,3,4,5,6]
43
+ throw.eq_or_above(3).should == 4
44
+ end
45
+
46
+ it "should return number of rolls below a certain value" do
47
+ throw = Throw.new [1,2,3,4,5,6]
48
+ throw.below(3).should == 2
49
+ end
50
+
51
+ it "should return number of rolls equal or above a value" do
52
+ throw = Throw.new [1,2,3,4,5,6]
53
+ throw.eq_or_below(3).should == 3
54
+ end
55
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ require 'spec'
6
+ end
7
+ begin
8
+ require 'spec/rake/spectask'
9
+ rescue LoadError
10
+ puts <<-EOS
11
+ To use rspec for testing you must install rspec gem:
12
+ gem install rspec
13
+ EOS
14
+ exit(0)
15
+ end
16
+
17
+ desc "Run the specs under spec/models"
18
+ Spec::Rake::SpecTask.new do |t|
19
+ t.spec_opts = ['--options', "spec/spec.opts"]
20
+ t.spec_files = FileList['spec/**/*_spec.rb']
21
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rudil
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marc-Antonio Bisotti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-11 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.3
24
+ version:
25
+ description: rudil is a dice library for ruby.
26
+ email:
27
+ - mail@marc-antonio.de
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ files:
36
+ - History.txt
37
+ - Manifest.txt
38
+ - README.rdoc
39
+ - Rakefile
40
+ - lib/rudil.rb
41
+ - lib/rudil/die.rb
42
+ - lib/rudil/throw.rb
43
+ - script/console
44
+ - script/destroy
45
+ - script/generate
46
+ - spec/die_spec.rb
47
+ - spec/spec.opts
48
+ - spec/spec_helper.rb
49
+ - spec/throw_spec.rb
50
+ - tasks/rspec.rake
51
+ has_rdoc: true
52
+ homepage: http://github.com/sMAshdot/rudil
53
+ licenses: []
54
+
55
+ post_install_message: PostInstall.txt
56
+ rdoc_options:
57
+ - --main
58
+ - README.rdoc
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ requirements: []
74
+
75
+ rubyforge_project: rudil
76
+ rubygems_version: 1.3.5
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: rudil is a dice library for ruby.
80
+ test_files: []
81
+