tbpgr_utils 0.0.120 → 0.0.121

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e1b70999803bbab64b871782e8db0234e4a2bdb0
4
- data.tar.gz: 9caf56eb84d02338a121dd04d846d24cb0cf097d
3
+ metadata.gz: def617e72b46baf4acea16f53a796d7b91a89d61
4
+ data.tar.gz: 9b4383ade5e36e6b6bd9534d41ff30889772b3d3
5
5
  SHA512:
6
- metadata.gz: 8f5f7a0730044402893300645e6a500ea0ba950b955f8de9899778665ff6d78238690f15eb49479d8ecf653c7b38d632fe185d2a1e4ab33b8658855e4592f2b8
7
- data.tar.gz: 5a61fe413bde0ebc771603c17aae78e3289c3d1d1a91982fbc025006df3dd8a33b5e35ce39c1f690edce023bbae97bc2ffc94630b31542714db663affce9ce0f
6
+ metadata.gz: bd9600a5cac06e3bf7733b438e01e75bab9fee1fd2a74926889cbfd4096cea725d40b9bd2c109187624ab1ad6e898ee2a1f6be114abdbbb633910523550fc11b
7
+ data.tar.gz: 077edd7c7f229bdb433d4e0df51a28ec434d9bb7bdb42f5f4ada56bfc9af9d0299dfc3f1549f377fadb0c0fce9b2bc3db9c6948a0765b4e76b3d0776fc3b0b13
data/README.md CHANGED
@@ -25,6 +25,7 @@ Or install it yourself as:
25
25
  | class/module/method | mean |
26
26
  |:----------- |:------------ |
27
27
  |[TbpgrUtils Array#>>](#array) |alias of map(&:method_symbol) |
28
+ |[TbpgrUtils Array#average](#arrayaverage) |return average |
28
29
  |[TbpgrUtils Array#kernel_send](#arraykernel_send) |alias of map {|v|send :some_kernel_method, v} |
29
30
  |[TbpgrUtils Array#sum](#arraysum) |alias of Array#reduce(&:+). |
30
31
  |[TbpgrUtils Array#to_table](#arrayto_table) |Array(Array, Array...) to table format. |
@@ -170,6 +171,16 @@ require 'tbpgr_utils'
170
171
 
171
172
  [back to list](#list)
172
173
 
174
+ ### Array#average
175
+ ~~~ruby
176
+ require 'tbpgr_utils'
177
+ [*1..6].average # => 3.5
178
+ [1.5, 2.5].average # => 2.0
179
+ [*'a'..'z'].average # => raise TypeError
180
+ ~~~
181
+
182
+ [back to list](#list)
183
+
173
184
  ### Array#kernel_send
174
185
  ~~~ruby
175
186
  require 'tbpgr_utils'
@@ -3556,6 +3567,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
3556
3567
  https://github.com/tbpgr/tbpgr_utils_snippets
3557
3568
 
3558
3569
  ## History
3570
+ * version 0.0.121 : add Array#average
3559
3571
  * version 0.0.120 : add Array#sum
3560
3572
  * version 0.0.119 : add Kernel#exchange
3561
3573
  * version 0.0.118 : add String#uniq
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ # Array
4
+ class Array
5
+ # return average
6
+ #
7
+ # === Example
8
+ #
9
+ # [*1..6].average # => 3.5
10
+ # [1.5, 2.5].average # => 2.0
11
+ # [*'a'..'z'].average # => raise TypeError
12
+ #
13
+ def average
14
+ fail TypeError, 'you have to use elements that is Numeric' if any? { |v|!v.is_a? Numeric }
15
+ reduce(0.0) { |r, v|r = r + v.to_f; r } / size
16
+ end
17
+ end
@@ -1,4 +1,5 @@
1
1
  # encoding: utf-8
2
+ require 'open_classes/array/average'
2
3
  require 'open_classes/array/gte_gte'
3
4
  require 'open_classes/array/kernel_send'
4
5
  require 'open_classes/array/sum'
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Tbpgr Utilities
4
4
  module TbpgrUtils
5
- VERSION = '0.0.120'
5
+ VERSION = '0.0.121'
6
6
  end
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'tbpgr_utils'
4
+
5
+ describe Array do
6
+ context :average do
7
+ cases = [
8
+ {
9
+ case_no: 1,
10
+ case_title: 'numbers(integer) case',
11
+ input: [*1..6],
12
+ expected: 3.5,
13
+ },
14
+ {
15
+ case_no: 2,
16
+ case_title: 'numbers(float) case',
17
+ input: [1.5, 2.5],
18
+ expected: 2.0,
19
+ },
20
+ {
21
+ case_no: 3,
22
+ case_title: 'not numbers case',
23
+ input: [*'a'..'z'],
24
+ expect_error: true,
25
+ },
26
+ ]
27
+
28
+ cases.each do |c|
29
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
30
+ begin
31
+ case_before c
32
+
33
+ # -- given --
34
+ # nothing
35
+
36
+ # -- when/then --
37
+ if c[:expect_error]
38
+ lambda { c[:input].average }.should raise_error(StandardError)
39
+ next
40
+ else
41
+ actual = c[:input].average
42
+ end
43
+
44
+ expect(actual).to eq(c[:expected])
45
+ ensure
46
+ case_after c
47
+ end
48
+ end
49
+
50
+ def case_before(c)
51
+ # implement each case before
52
+ end
53
+
54
+ def case_after(c)
55
+ # implement each case after
56
+ end
57
+ end
58
+ end
59
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tbpgr_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.120
4
+ version: 0.0.121
5
5
  platform: ruby
6
6
  authors:
7
7
  - tbpgr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-07 00:00:00.000000000 Z
11
+ date: 2014-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -135,6 +135,7 @@ files:
135
135
  - lib/markdown_string.rb
136
136
  - lib/metasyntactic_variable.rb
137
137
  - lib/open_classes/array.rb
138
+ - lib/open_classes/array/average.rb
138
139
  - lib/open_classes/array/gte_gte.rb
139
140
  - lib/open_classes/array/kernel_send.rb
140
141
  - lib/open_classes/array/sum.rb
@@ -270,6 +271,7 @@ files:
270
271
  - spec/markdown/ol_spec.rb
271
272
  - spec/markdown/ul_spec.rb
272
273
  - spec/metasyntactic_variable_spec.rb
274
+ - spec/open_classes/array/average_spec.rb
273
275
  - spec/open_classes/array/gte_gte_spec.rb
274
276
  - spec/open_classes/array/kernel_send_spec.rb
275
277
  - spec/open_classes/array/sum_spec.rb
@@ -421,6 +423,7 @@ test_files:
421
423
  - spec/markdown/ol_spec.rb
422
424
  - spec/markdown/ul_spec.rb
423
425
  - spec/metasyntactic_variable_spec.rb
426
+ - spec/open_classes/array/average_spec.rb
424
427
  - spec/open_classes/array/gte_gte_spec.rb
425
428
  - spec/open_classes/array/kernel_send_spec.rb
426
429
  - spec/open_classes/array/sum_spec.rb