ruby_percentrank 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 20ecec3f5c8522c3a82bbbdc81b5f99faa82369f
4
+ data.tar.gz: 2c79a93e8b001a8ef92ce9613233f3ccd63ec86a
5
+ SHA512:
6
+ metadata.gz: d2ae4c0fa101bdb53b04e7b658adeeae089c528e4c49693511296b110befd22019cf72ac58a668e2e408d8f7b45dcc0ba3fbc73bd4f589e75da16ce64f7b94ea
7
+ data.tar.gz: 0ca82260e3e3dbf859ce1a810b9e46148e1a6db07991190cc6953e122601f3f8e11636692e403dd6a050d11b8d1a2eb49821dc8ff9d248ec2946eefa2ef5fa8c
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in percentrank.gemspec
4
+ gemspec
@@ -0,0 +1,37 @@
1
+ # Percentrank
2
+
3
+ Add Array#percentrank(value) and Range#percentrank(value).
4
+
5
+ Function description, See [PERCENTRANK function - Excel - Office.com](http://office.microsoft.com/en-us/excel-help/percentrank-function-HP010335656.aspx)
6
+ (in Japanese: [PERCENTRANK 関数 - Excel - Office.com](http://office.microsoft.com/ja-jp/excel-help/HP010335656.aspx)).
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'percentrank'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install percentrank
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ require 'percentrank'
26
+
27
+ ([125, 150, 160, 90]).percentrank(125) #=> 0.33.
28
+ ([12 , 13, 14, 10]).percentrank(14) #=> 1 .
29
+ ```
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
@@ -0,0 +1,38 @@
1
+ require "percentrank/version"
2
+
3
+ module Percentrank
4
+ def percentrank(n)
5
+ min, max = self.minmax.map(&:to_f)
6
+ (n - min) / (max - min)
7
+ end
8
+
9
+ def percentrank(number, significance = 2)
10
+ array = self.sort
11
+
12
+ return (array.index(number).to_f / (array.count - 1).to_f).abs.round(significance) if array.include?(number)
13
+
14
+ x1, x2, y1, y2 = 0.0
15
+ array.each_with_index do |element, index|
16
+ if element < number && number < array[index + 1]
17
+ x1 = element
18
+ x2 = array[index + 1]
19
+ y1 = percentrank(array, x1)
20
+ y2 = percentrank(array, x2)
21
+
22
+ return (((x2 - number) * y1 + (number - x1) * y2) / (x1 - x2)).abs.round(significance)
23
+ end
24
+ end
25
+ return nil
26
+ rescue
27
+ nil
28
+ end
29
+ end
30
+
31
+ class Range
32
+ include Percentrank
33
+ end
34
+
35
+ class Array
36
+ include Percentrank
37
+ end
38
+
@@ -0,0 +1,3 @@
1
+ module Percentrank
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'percentrank/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby_percentrank"
8
+ spec.version = Percentrank::VERSION
9
+ spec.authors = ["khacluan"]
10
+ spec.email = ["dangluan20@gmail.com"]
11
+ spec.description = %q{Add Array#percentrank(value) and Range#percentrank(value).}
12
+ spec.summary = %q{This gem used to calculate excel's percentrank in ruby}
13
+ spec.homepage = "http://khacluan.github.io"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "rspec"
22
+ end
@@ -0,0 +1,35 @@
1
+ require 'rspec'
2
+ require 'percentrank'
3
+
4
+ describe Percentrank do
5
+ describe "Range#percentrank" do
6
+ it "within range" do
7
+ (100..200).percentrank(120).should be_within(0.001).of(0.2)
8
+ (100..200).percentrank(140).should be_within(0.001).of(0.4)
9
+ end
10
+ it "over range" do
11
+ (100..200).percentrank(220).should be_within(0.001).of(1.2)
12
+ (100..200).percentrank(350).should be_within(0.001).of(2.5)
13
+ end
14
+ it "under range" do
15
+ (100..200).percentrank(20).should be_within(0.001).of(-0.8)
16
+ (100..200).percentrank(0).should be_within(0.001).of(-1.0)
17
+ end
18
+ end
19
+
20
+ describe "Array#percentrank" do
21
+ it "within range" do
22
+ [100, 130, 200, 185].percentrank(120).should be_within(0.001).of(0.2)
23
+ [100, 130, 200, 185].percentrank(140).should be_within(0.001).of(0.4)
24
+ end
25
+ it "over range" do
26
+ [100, 130, 200, 185].percentrank(220).should be_within(0.001).of(1.2)
27
+ [100, 130, 200, 185].percentrank(350).should be_within(0.001).of(2.5)
28
+ end
29
+ it "under range" do
30
+ [100, 130, 200, 185].percentrank(20).should be_within(0.001).of(-0.8)
31
+ [100, 130, 200, 185].percentrank(0).should be_within(0.001).of(-1.0)
32
+ end
33
+ end
34
+ end
35
+
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_percentrank
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - khacluan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Add Array#percentrank(value) and Range#percentrank(value).
28
+ email:
29
+ - dangluan20@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - README.md
37
+ - lib/percentrank.rb
38
+ - lib/percentrank/version.rb
39
+ - percentrank.gemspec
40
+ - spec/percentrank_spec.rb
41
+ homepage: http://khacluan.github.io
42
+ licenses:
43
+ - MIT
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 2.4.4
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: This gem used to calculate excel's percentrank in ruby
65
+ test_files:
66
+ - spec/percentrank_spec.rb