chansu 0.1.0
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.
- checksums.yaml +7 -0
- data/.rspec +1 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +4 -0
- data/lib/chansu/dsl.rb +69 -0
- data/lib/chansu/loops.rb +32 -0
- data/lib/chansu/toss.rb +39 -0
- data/lib/chansu/version.rb +5 -0
- data/lib/chansu.rb +100 -0
- data/sig/chansu.rbs +4 -0
- data/spec/chansu_spec.rb +48 -0
- data/spec/spec_helper.rb +98 -0
- metadata +64 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 0f4ba8ed2317017e71a0eb192f19947a7792fcb6eb43eb12107a803d69d7a62b
|
|
4
|
+
data.tar.gz: 88b4ad19c077b7d43b3a87f72ab4ccc35fc566d02e14fd7f5eca26d28904d8b6
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 67558f2e8bb41692107016f049488e0b4e417f2f84aac4fba67837f7165528b4711998f8a99a35319fea898f46799e7f12f876291f1a72e3792f8a01ce90698d
|
|
7
|
+
data.tar.gz: 491b9dd2d5ec2a1566febfc4a3a87a98f53d7bfd1f10c7c9fc267fd71e5901c64e44f3000646ee278e50ffb1cc8adf4b48f6f37c4e4f79ebbaa05cb1ab96c324
|
data/.rspec
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--require spec_helper
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
|
93
|
+
ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Santiago Calderon
|
|
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
|
|
13
|
+
all 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
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Chansu
|
|
2
|
+
|
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
|
4
|
+
|
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/chansu`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
10
|
+
|
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
TODO: Write usage instructions here
|
|
26
|
+
|
|
27
|
+
## Development
|
|
28
|
+
|
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
30
|
+
|
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
32
|
+
|
|
33
|
+
## Contributing
|
|
34
|
+
|
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/chansu. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/chansu/blob/master/CODE_OF_CONDUCT.md).
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
40
|
+
|
|
41
|
+
## Code of Conduct
|
|
42
|
+
|
|
43
|
+
Everyone interacting in the Chansu project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/chansu/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/lib/chansu/dsl.rb
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# DSL for the table of common probability words.
|
|
4
|
+
# If you are cronically online you will get this
|
|
5
|
+
module Chansu
|
|
6
|
+
def always
|
|
7
|
+
chance(1.0) { yield if block_given? }
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def certainly
|
|
11
|
+
chance(0.9) { yield if block_given? }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def almost_always
|
|
15
|
+
chance(0.85) { yield if block_given? }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def usually
|
|
19
|
+
chance(0.8) { yield if block_given? }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def often
|
|
23
|
+
chance(0.75) { yield if block_given? }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def likely
|
|
27
|
+
chance(0.7) { yield if block_given? }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def frequently
|
|
31
|
+
chance(0.65) { yield if block_given? }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def probably
|
|
35
|
+
chance(0.6) { yield if block_given? }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def more_often_than_not
|
|
39
|
+
chance(0.6) { yield if block_given? }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def maybe
|
|
43
|
+
chance(0.5) { yield if block_given? }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def possibly
|
|
47
|
+
chance(0.4) { yield if block_given? }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def not_often
|
|
51
|
+
chance(0.35) { yield if block_given? }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def unlikely
|
|
55
|
+
chance(0.25) { yield if block_given? }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def with_low_probability
|
|
59
|
+
chance(0.15) { yield if block_given? }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def rarely
|
|
63
|
+
chance(0.1) { yield if block_given? }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def never
|
|
67
|
+
chance(0.00001) { yield if block_given? } # never say never
|
|
68
|
+
end
|
|
69
|
+
end
|
data/lib/chansu/loops.rb
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Block runner with probability stops
|
|
4
|
+
module Chansu
|
|
5
|
+
def self.until_success(chance: 0.5, max_attempts: 10)
|
|
6
|
+
raise ArgumentError, 'max_attempts must be greater than 0' unless max_attempts.positive?
|
|
7
|
+
raise ArgumentError, 'chance must be between 0.0 and 1.0' unless chance.between?(0.0, 1.0)
|
|
8
|
+
raise ArgumentError, 'A block must be provided' unless block_given?
|
|
9
|
+
|
|
10
|
+
attempts = 0
|
|
11
|
+
until chance(chance)
|
|
12
|
+
attempts += 1
|
|
13
|
+
yield attempts
|
|
14
|
+
break if attempts >= max_attempts
|
|
15
|
+
end
|
|
16
|
+
attempts
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.until_failure(chance: 0.5, max_attempts: 10)
|
|
20
|
+
raise ArgumentError, 'max_attempts must be greater than 0' unless max_attempts.positive?
|
|
21
|
+
raise ArgumentError, 'chance must be between 0.0 and 1.0' unless chance.between?(0.0, 1.0)
|
|
22
|
+
raise ArgumentError, 'A block must be provided' unless block_given?
|
|
23
|
+
|
|
24
|
+
attempts = 0
|
|
25
|
+
while chance(chance)
|
|
26
|
+
attempts += 1
|
|
27
|
+
yield attempts
|
|
28
|
+
break if attempts >= max_attempts
|
|
29
|
+
end
|
|
30
|
+
attempts
|
|
31
|
+
end
|
|
32
|
+
end
|
data/lib/chansu/toss.rb
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# For everything that is throwed in the air expecting a result
|
|
4
|
+
module Chansu
|
|
5
|
+
def self.coin
|
|
6
|
+
chance(0.5) ? :heads : :tails
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def self.dice(sides = 6, times = 1) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
|
10
|
+
case sides
|
|
11
|
+
when String
|
|
12
|
+
expr = sides
|
|
13
|
+
match = expr.match(/(\d+)d(\d+)([+-]\d+)?/)
|
|
14
|
+
raise ArgumentError, "Invalid dice expression: #{expr}" unless match
|
|
15
|
+
|
|
16
|
+
times, sides, modifier = match.captures
|
|
17
|
+
rolls = Array.new(times.to_i) { rand(1..sides.to_i) }
|
|
18
|
+
total = rolls.sum + (modifier ? modifier.to_i : 0)
|
|
19
|
+
|
|
20
|
+
{ rolls: rolls, total: total }
|
|
21
|
+
|
|
22
|
+
when Integer
|
|
23
|
+
raise ArgumentError, 'Die must have at least 1 side' if sides < 1
|
|
24
|
+
|
|
25
|
+
if times == 1
|
|
26
|
+
rand(1..sides)
|
|
27
|
+
else
|
|
28
|
+
Array.new(times) { rand(1..sides) }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
else
|
|
32
|
+
raise ArgumentError, "Unsupported dice argument: #{sides.inspect}"
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
class << self
|
|
37
|
+
alias roll dice
|
|
38
|
+
end
|
|
39
|
+
end
|
data/lib/chansu.rb
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'chansu/version'
|
|
4
|
+
require_relative 'chansu/dsl'
|
|
5
|
+
require_relative 'chansu/loops'
|
|
6
|
+
require_relative 'chansu/toss'
|
|
7
|
+
|
|
8
|
+
# DSL for chance, probability, randomness
|
|
9
|
+
module Chansu
|
|
10
|
+
@globals_enabled = false
|
|
11
|
+
|
|
12
|
+
@dsl_methods = %i[chance
|
|
13
|
+
always
|
|
14
|
+
certainly
|
|
15
|
+
almost_always
|
|
16
|
+
usually
|
|
17
|
+
often
|
|
18
|
+
likely
|
|
19
|
+
frequently
|
|
20
|
+
probably
|
|
21
|
+
more_often_than_not
|
|
22
|
+
maybe
|
|
23
|
+
possibly
|
|
24
|
+
not_often
|
|
25
|
+
unlikely
|
|
26
|
+
with_low_probability
|
|
27
|
+
rarely
|
|
28
|
+
never
|
|
29
|
+
until_success
|
|
30
|
+
until_failure]
|
|
31
|
+
|
|
32
|
+
@dice_methods = %i[coin
|
|
33
|
+
dice
|
|
34
|
+
roll]
|
|
35
|
+
|
|
36
|
+
def self.enable_globals! # rubocop:disable Metrics/MethodLength
|
|
37
|
+
return if @globals_enabled
|
|
38
|
+
|
|
39
|
+
(@dsl_methods + @dice_methods).each do |method|
|
|
40
|
+
Kernel.define_method(method) do |*args, &block|
|
|
41
|
+
Chansu.send(method, *args, &block)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
[4, 6, 8, 10, 12, 20, 100].each do |sides|
|
|
46
|
+
Kernel.define_method("d#{sides}") do |times = 1|
|
|
47
|
+
Chansu.dice(sides, times)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
@globals_enabled = true
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def self.disable_globals!
|
|
55
|
+
return unless @globals_enabled
|
|
56
|
+
|
|
57
|
+
(@dsl_methods + @dice_methods).each do |method|
|
|
58
|
+
Kernel.send(:remove_method, method) if Kernel.method_defined?(method)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
[4, 6, 8, 10, 12, 20, 100].each do |sides|
|
|
62
|
+
Kernel.send(:remove_method, "d#{sides}".to_sym) if Kernel.method_defined?("d#{sides}".to_sym)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
@globals_enabled = false
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def self.chance(prob = 0.5) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
|
|
69
|
+
probability =
|
|
70
|
+
case prob
|
|
71
|
+
when String
|
|
72
|
+
raise ArgumentError, 'Invalid string format for probability' unless prob.end_with?('%')
|
|
73
|
+
|
|
74
|
+
prob.to_f / 100.0
|
|
75
|
+
|
|
76
|
+
when Integer
|
|
77
|
+
raise ArgumentError, 'Integer probability must be between 0 and 100' unless prob.between?(0, 100)
|
|
78
|
+
|
|
79
|
+
prob.to_f / 100.0
|
|
80
|
+
|
|
81
|
+
when Float
|
|
82
|
+
raise ArgumentError, 'Float probability must be between 0.0 and 1.0' unless prob.between?(0.0, 1.0)
|
|
83
|
+
|
|
84
|
+
prob
|
|
85
|
+
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
passed = rand < probability
|
|
89
|
+
|
|
90
|
+
yield if passed && block_given?
|
|
91
|
+
|
|
92
|
+
passed
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
Chansu.enable_globals!
|
|
97
|
+
|
|
98
|
+
module Kernel
|
|
99
|
+
include Chansu
|
|
100
|
+
end
|
data/sig/chansu.rbs
ADDED
data/spec/chansu_spec.rb
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# spec/chansu_spec.rb
|
|
4
|
+
require 'chansu'
|
|
5
|
+
|
|
6
|
+
RSpec.describe Chansu do # rubocop:disable Metrics/BlockLength
|
|
7
|
+
describe '.chance' do
|
|
8
|
+
it 'accepts percentage strings' do
|
|
9
|
+
100.times do
|
|
10
|
+
expect([true, false]).to include(Chansu.chance('50%'))
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it 'accepts decimal floats' do
|
|
15
|
+
100.times do
|
|
16
|
+
expect([true, false]).to include(Chansu.chance(0.5))
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it 'accepts integers as percents' do
|
|
21
|
+
100.times do
|
|
22
|
+
expect([true, false]).to include(Chansu.chance(25))
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
describe '.dice' do
|
|
28
|
+
it 'rolls a single die' do
|
|
29
|
+
50.times do
|
|
30
|
+
expect(Chansu.dice(6)).to be_between(1, 6)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it 'rolls multiple dice' do
|
|
35
|
+
rolls = Chansu.dice(6, 3)
|
|
36
|
+
expect(rolls.size).to eq(3)
|
|
37
|
+
expect(rolls).to all(be_between(1, 6))
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
describe '.roll (alias of .dice)' do
|
|
42
|
+
it 'parses dice expressions' do
|
|
43
|
+
result = Chansu.roll('2d6+3')
|
|
44
|
+
expect(result[:rolls].size).to eq(2)
|
|
45
|
+
expect(result[:total]).to be >= 5
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
|
4
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
|
5
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
|
6
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
|
7
|
+
# files.
|
|
8
|
+
#
|
|
9
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
|
10
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
|
11
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
|
12
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
|
13
|
+
# a separate helper file that requires the additional dependencies and performs
|
|
14
|
+
# the additional setup, and require it from the spec files that actually need
|
|
15
|
+
# it.
|
|
16
|
+
#
|
|
17
|
+
# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
|
18
|
+
RSpec.configure do |config|
|
|
19
|
+
# rspec-expectations config goes here. You can use an alternate
|
|
20
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
|
21
|
+
# assertions if you prefer.
|
|
22
|
+
config.expect_with :rspec do |expectations|
|
|
23
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
|
24
|
+
# and `failure_message` of custom matchers include text for helper methods
|
|
25
|
+
# defined using `chain`, e.g.:
|
|
26
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
|
27
|
+
# # => "be bigger than 2 and smaller than 4"
|
|
28
|
+
# ...rather than:
|
|
29
|
+
# # => "be bigger than 2"
|
|
30
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
|
34
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
|
35
|
+
config.mock_with :rspec do |mocks|
|
|
36
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
|
37
|
+
# a real object. This is generally recommended, and will default to
|
|
38
|
+
# `true` in RSpec 4.
|
|
39
|
+
mocks.verify_partial_doubles = true
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
|
43
|
+
# have no way to turn it off -- the option exists only for backwards
|
|
44
|
+
# compatibility in RSpec 3). It causes shared context metadata to be
|
|
45
|
+
# inherited by the metadata hash of host groups and examples, rather than
|
|
46
|
+
# triggering implicit auto-inclusion in groups with matching metadata.
|
|
47
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
|
48
|
+
|
|
49
|
+
# The settings below are suggested to provide a good initial experience
|
|
50
|
+
# with RSpec, but feel free to customize to your heart's content.
|
|
51
|
+
# # This allows you to limit a spec run to individual examples or groups
|
|
52
|
+
# # you care about by tagging them with `:focus` metadata. When nothing
|
|
53
|
+
# # is tagged with `:focus`, all examples get run. RSpec also provides
|
|
54
|
+
# # aliases for `it`, `describe`, and `context` that include `:focus`
|
|
55
|
+
# # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
|
56
|
+
# config.filter_run_when_matching :focus
|
|
57
|
+
#
|
|
58
|
+
# # Allows RSpec to persist some state between runs in order to support
|
|
59
|
+
# # the `--only-failures` and `--next-failure` CLI options. We recommend
|
|
60
|
+
# # you configure your source control system to ignore this file.
|
|
61
|
+
# config.example_status_persistence_file_path = "spec/examples.txt"
|
|
62
|
+
#
|
|
63
|
+
# # Limits the available syntax to the non-monkey patched syntax that is
|
|
64
|
+
# # recommended. For more details, see:
|
|
65
|
+
# # https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/
|
|
66
|
+
# config.disable_monkey_patching!
|
|
67
|
+
#
|
|
68
|
+
# # This setting enables warnings. It's recommended, but in some cases may
|
|
69
|
+
# # be too noisy due to issues in dependencies.
|
|
70
|
+
# config.warnings = true
|
|
71
|
+
#
|
|
72
|
+
# # Many RSpec users commonly either run the entire suite or an individual
|
|
73
|
+
# # file, and it's useful to allow more verbose output when running an
|
|
74
|
+
# # individual spec file.
|
|
75
|
+
# if config.files_to_run.one?
|
|
76
|
+
# # Use the documentation formatter for detailed output,
|
|
77
|
+
# # unless a formatter has already been configured
|
|
78
|
+
# # (e.g. via a command-line flag).
|
|
79
|
+
# config.default_formatter = "doc"
|
|
80
|
+
# end
|
|
81
|
+
#
|
|
82
|
+
# # Print the 10 slowest examples and example groups at the
|
|
83
|
+
# # end of the spec run, to help surface which specs are running
|
|
84
|
+
# # particularly slow.
|
|
85
|
+
# config.profile_examples = 10
|
|
86
|
+
#
|
|
87
|
+
# # Run specs in random order to surface order dependencies. If you find an
|
|
88
|
+
# # order dependency and want to debug it, you can fix the order by providing
|
|
89
|
+
# # the seed, which is printed after each run.
|
|
90
|
+
# # --seed 1234
|
|
91
|
+
# config.order = :random
|
|
92
|
+
#
|
|
93
|
+
# # Seed global randomization in this process using the `--seed` CLI option.
|
|
94
|
+
# # Setting this allows you to use `--seed` to deterministically reproduce
|
|
95
|
+
# # test failures related to randomization by passing the same `--seed` value
|
|
96
|
+
# # as the one that triggered the failure.
|
|
97
|
+
# Kernel.srand config.seed
|
|
98
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: chansu
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Santiago Calderon
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: |-
|
|
13
|
+
\
|
|
14
|
+
Chansu is a Ruby DSL for working with randomness and probability.
|
|
15
|
+
It provides helpers like `chance("35%")`, `dice(6, 3)`, and RPG-style
|
|
16
|
+
aliases such as `d20` and `d100`. It also includes higher-level
|
|
17
|
+
constructs like `often { ... }`, `rarely { ... }`, and `until_success`
|
|
18
|
+
for expressive probabilistic logic. Useful for games, simulations,
|
|
19
|
+
and playful Ruby experiments.
|
|
20
|
+
email:
|
|
21
|
+
- escalderongv@gmail.com
|
|
22
|
+
executables: []
|
|
23
|
+
extensions: []
|
|
24
|
+
extra_rdoc_files: []
|
|
25
|
+
files:
|
|
26
|
+
- ".rspec"
|
|
27
|
+
- CHANGELOG.md
|
|
28
|
+
- CODE_OF_CONDUCT.md
|
|
29
|
+
- LICENSE.txt
|
|
30
|
+
- README.md
|
|
31
|
+
- Rakefile
|
|
32
|
+
- lib/chansu.rb
|
|
33
|
+
- lib/chansu/dsl.rb
|
|
34
|
+
- lib/chansu/loops.rb
|
|
35
|
+
- lib/chansu/toss.rb
|
|
36
|
+
- lib/chansu/version.rb
|
|
37
|
+
- sig/chansu.rbs
|
|
38
|
+
- spec/chansu_spec.rb
|
|
39
|
+
- spec/spec_helper.rb
|
|
40
|
+
homepage: https://github.com/escalderong/chansu
|
|
41
|
+
licenses:
|
|
42
|
+
- MIT
|
|
43
|
+
metadata:
|
|
44
|
+
homepage_uri: https://github.com/escalderong/chansu
|
|
45
|
+
source_code_uri: https://github.com/escalderong/chansu
|
|
46
|
+
changelog_uri: https://github.com/escalderong/chansu/blob/master/CHANGELOG.md
|
|
47
|
+
rdoc_options: []
|
|
48
|
+
require_paths:
|
|
49
|
+
- lib
|
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: 3.2.0
|
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - ">="
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: '0'
|
|
60
|
+
requirements: []
|
|
61
|
+
rubygems_version: 3.7.1
|
|
62
|
+
specification_version: 4
|
|
63
|
+
summary: A Ruby DSL for probabilities, randomness, and dice.
|
|
64
|
+
test_files: []
|