roulette-wheel-selection 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/Gemfile +11 -0
- data/README.md +70 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/roulette-wheel-selection.rb +31 -0
- data/lib/roulette-wheel-selection/version.rb +3 -0
- data/roulette-wheel-selection.gemspec +33 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6a78fcb0bdb24004f33987d7aab36db84593df5ebe7f63d5854825b8150f5ac1
|
4
|
+
data.tar.gz: d67c64c8dc3d2d2279a9b51e603f3f817ddeeebd7808a554e632bd627ef858ac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e78215cefe900d74f2331053fe051e30adc586b9579cd07f80c622829ea71c8a2285f2bc377e1d4709952843ba1033b5b050455e2cefd1880c49abf1a15fee89
|
7
|
+
data.tar.gz: f3d39fd89c2e12e489a1c312657dad6cbb4042dcdfaae107b38fa8fb443f4c3967bbb09b74691dd878da68d760a6a57c96e7bdc092742c18983689c713c281df
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
/coverage/
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
[![Gem Version](https://img.shields.io/gem/v/roulette-wheel-selection.svg?style=flat)](https://rubygems.org/gems/roulette-wheel-selection)
|
2
|
+
[![Build Status](https://travis-ci.org/khiav223577/roulette-wheel-selection.svg?branch=master)](https://travis-ci.org/khiav223577/roulette-wheel-selection)
|
3
|
+
[![RubyGems](http://img.shields.io/gem/dt/roulette-wheel-selection.svg?style=flat)](https://rubygems.org/gems/roulette-wheel-selection)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/khiav223577/roulette-wheel-selection/badges/gpa.svg)](https://codeclimate.com/github/khiav223577/roulette-wheel-selection)
|
5
|
+
[![Test Coverage](https://codeclimate.com/github/khiav223577/roulette-wheel-selection/badges/coverage.svg)](https://codeclimate.com/github/khiav223577/roulette-wheel-selection/coverage)
|
6
|
+
|
7
|
+
# roulette-wheel-selection
|
8
|
+
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'roulette-wheel-selection'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install roulette-wheel-selection
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
### require the library
|
29
|
+
```rb
|
30
|
+
require 'roulette-wheel-selection'
|
31
|
+
```
|
32
|
+
|
33
|
+
### Hash format
|
34
|
+
```rb
|
35
|
+
RouletteWheelSelection.sample(
|
36
|
+
'cola' => 40,
|
37
|
+
'sprite' => 20,
|
38
|
+
'fanta' => 30,
|
39
|
+
'wine' => 40,
|
40
|
+
)
|
41
|
+
# => 'cola'
|
42
|
+
```
|
43
|
+
|
44
|
+
### Array format
|
45
|
+
```rb
|
46
|
+
drinks = [
|
47
|
+
{ name: 'cola', weight: 40 },
|
48
|
+
{ name: 'sprite', weight: 20 },
|
49
|
+
{ name: 'fanta', weight: 30 },
|
50
|
+
{ name: 'wine', weight: 40 },
|
51
|
+
]
|
52
|
+
RouletteWheelSelection.sample(drinks, :weight)
|
53
|
+
# => 'cola'
|
54
|
+
```
|
55
|
+
|
56
|
+
## Development
|
57
|
+
|
58
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
59
|
+
|
60
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/khiav223577/roulette-wheel-selection. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
65
|
+
|
66
|
+
|
67
|
+
## License
|
68
|
+
|
69
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
70
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "left_joins"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require "roulette-wheel-selection/version"
|
2
|
+
|
3
|
+
class RouletteWheelSelection
|
4
|
+
class << self
|
5
|
+
def sample(*args)
|
6
|
+
object = args.first
|
7
|
+
case object
|
8
|
+
when Array ; sample_from_array(*args)
|
9
|
+
when Hash ; sample_from_hash(*args)
|
10
|
+
else ; fail "Unsupported type: #{object.class}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def sample_from_array(array, key)
|
17
|
+
hash = array.map{|v| [v, v[key]] }.to_h
|
18
|
+
return sample_from_hash(hash)
|
19
|
+
end
|
20
|
+
|
21
|
+
def sample_from_hash(hash)
|
22
|
+
total_rate = hash.values.inject(&:+)
|
23
|
+
random_seed = rand(total_rate)
|
24
|
+
hash.each do |obj, rate|
|
25
|
+
return obj if random_seed < rate
|
26
|
+
random_seed -= rate
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'roulette-wheel-selection/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "roulette-wheel-selection"
|
8
|
+
spec.version = RouletteWheelSelection::VERSION
|
9
|
+
spec.authors = ["khiav reoy"]
|
10
|
+
spec.email = ["mrtmrt15xn@yahoo.com.tw"]
|
11
|
+
|
12
|
+
spec.summary = %q{}
|
13
|
+
spec.description = %q{}
|
14
|
+
spec.homepage = "https://github.com/khiav223577/roulette-wheel-selection"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
#if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
#else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
#end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
31
|
+
spec.add_development_dependency "rake", "~> 12.0"
|
32
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: roulette-wheel-selection
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- khiav reoy
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '12.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '12.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: ''
|
56
|
+
email:
|
57
|
+
- mrtmrt15xn@yahoo.com.tw
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- README.md
|
65
|
+
- Rakefile
|
66
|
+
- bin/console
|
67
|
+
- bin/setup
|
68
|
+
- lib/roulette-wheel-selection.rb
|
69
|
+
- lib/roulette-wheel-selection/version.rb
|
70
|
+
- roulette-wheel-selection.gemspec
|
71
|
+
homepage: https://github.com/khiav223577/roulette-wheel-selection
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.7.6
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: ''
|
95
|
+
test_files: []
|