probs 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 94e8ff153aa36052037039cc3909fdb2b6477802
4
+ data.tar.gz: 6394a0652d6e336a6cb3296f249af7a8b34a0396
5
+ SHA512:
6
+ metadata.gz: 246b962450c249ed6df2656b344916147925331a1aa942a2ed06dc48528e2be3ff5783802d711785156c12d789071bff03f5c6eac4e05e96cb1d64abad20f3ef
7
+ data.tar.gz: 01a18286fe56d2f260045cdec76525b812d81741413f4c459315d9e13d220b009fe9861e71aa2282f8207b466a5d9ac2fcd134df9e6802cf0aebaf28eb29aaab
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Nathan Kidd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # Probs
2
+
3
+ A gem which aims to add helpful methods and classes for dealing with chance/probability.
4
+
5
+ ## Usage
6
+
7
+ 75.percent == 3.in(4) == Rational(3,4).to_probability == Probs.new(3,4)
8
+ # => true
9
+
10
+ puts 75.percent
11
+ # 75.0%
12
+
13
+ ## Examples
14
+
15
+ Calculate the percentage of drawing a certain suit from a deck of cards.
16
+
17
+ Probs.new(13, 52).percent
18
+ # => 25.0
data/lib/probs.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'forwardable'
2
+
3
+ require 'probs/core_extensions/integer'
4
+ require 'probs/core_extensions/rational'
5
+ require 'probs/probability'
6
+ require 'probs/chance'
7
+
8
+ module Probs
9
+ def self.new(*args)
10
+ Probability.new(*args)
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Probs
2
+ class Chance
3
+ def initialize(probability, &block)
4
+ @probability = probability
5
+ yield if block_given? and happen?
6
+ end
7
+
8
+ def happen?
9
+ @probability.percent > Kernel.rand(100)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ class Integer
2
+ # Syntactic sugar for creating probability objects.
3
+ #
4
+ # 3.in(6) => 50.0%
5
+ def in(number)
6
+ Probs.new(self, number)
7
+ end
8
+
9
+ # Syntactic sugar for creating probability objects.
10
+ #
11
+ def percent
12
+ Probs.new(self.to_f / 100)
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ class Rational
2
+ def to_probability
3
+ Probs.new(numerator, denominator)
4
+ end
5
+ end
@@ -0,0 +1,33 @@
1
+ module Probs
2
+ class Probability
3
+ extend Forwardable
4
+ def_delegators :@rational, :numerator, :denominator
5
+
6
+ def initialize(*args)
7
+ @rational = Rational(*args)
8
+ end
9
+ def decimal
10
+ numerator.to_f / denominator
11
+ end
12
+ def percentage
13
+ decimal.to_f * 100
14
+ end
15
+
16
+ def to_s(format=:percentage)
17
+ case format
18
+ when :decimal
19
+ decimal.to_s
20
+ when :fraction
21
+ "#{numerator}/#{denominator}"
22
+ when :percentage
23
+ percentage.to_s + '%'
24
+ when :ratio
25
+ "#{numerator}:#{denominator - numerator}"
26
+ end
27
+ end
28
+
29
+ def chance(&block)
30
+ Chance.new(self, &block)
31
+ end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: probs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Kidd
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE
20
+ - README.md
21
+ - lib/probs.rb
22
+ - lib/probs/chance.rb
23
+ - lib/probs/core_extensions/integer.rb
24
+ - lib/probs/core_extensions/rational.rb
25
+ - lib/probs/probability.rb
26
+ homepage: https://github.com/bksuperking/probs
27
+ licenses:
28
+ - MIT
29
+ metadata:
30
+ source_code_uri: https://github.com/bksuperking/probs
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.6.14
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Representing probabilities with objects
51
+ test_files: []