rb_ext 0.3.1 → 0.4.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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
data/lib/rb_ext/array.rb CHANGED
@@ -1,12 +1,12 @@
1
1
  module RbExt
2
2
  module Array
3
+
3
4
  # Splits an array by _separator_ and returns an array holding
4
5
  # the splitted values in arrays. Works like String#split for arrays.
5
6
  #
6
7
  # -- TODO: modify to support nested arrays
7
8
  #
8
9
  # Example:
9
- #
10
10
  # a = %w(a b c - d e f g - h i j - k)
11
11
  # a.split("-") # => [[a, b, c], [d, e, f, g], [h, i, j] [k]]
12
12
  #
@@ -40,7 +40,6 @@ module RbExt
40
40
  # and supports nested arrays.
41
41
  #
42
42
  # Example:
43
- #
44
43
  # a = ["a", "b", 1, "c", 2.5, ["d", 3], "e"]
45
44
  # a.n_up(2) # => ["aa", "bb", 2, "cc", 5.0, ["dd", 6], "ee"]
46
45
  #
@@ -63,7 +62,6 @@ module RbExt
63
62
  # Flattens first level of an array.
64
63
  #
65
64
  # Example:
66
- #
67
65
  # [1, [2, [3]]].flatten_once # => [1, 2, [3]]
68
66
  #
69
67
  def flatten_top
@@ -76,13 +74,18 @@ module RbExt
76
74
  end
77
75
  end
78
76
 
77
+
78
+
79
+ ########################################################
80
+ ################# TYPE MAPPING METHODS #################
81
+ ########################################################
82
+
79
83
  # Returns a hash where its keys are the string versions
80
84
  # of the values in the array and invokes a block for each
81
85
  # element in the array taking the return value of the block
82
86
  # as the value for the hash element.
83
87
  #
84
88
  # Example:
85
- #
86
89
  # ["a", "b", "c"].to_hash_keys { |arr, e| arr.index(e) } # => {"a"=>0, "b"=>1, "c"=>2}
87
90
  # ["a", "b", "c", ["d", "e"]].to_hash_keys { |arr, e| arr.index(e) } # => {"a"=>0, "b"=>1, "c"=>2, "[\"d\", \"e\"]"=>{"d"=>0, "e"=>1}}
88
91
  #
@@ -106,7 +109,6 @@ module RbExt
106
109
  # as the key for the hash element.
107
110
  #
108
111
  # Example:
109
- #
110
112
  # ["a", "b", "c"].to_hash_values { |arr, e| arr.index(e) } # => {"0"=>"a", "1"=>"b", "2"=>"c"}
111
113
  # ["a", "b", "c", ["d", "e"]].to_hash_values { |arr, e| arr.index(e) } # => {"0"=>"a", "1"=>"b", "2"=>"c", "{\"0\"=>\"d\", \"1\"=>\"e\"}"=>["d", "e"]}
112
114
  #
@@ -130,7 +132,6 @@ module RbExt
130
132
  # as a single hash.
131
133
  #
132
134
  # Example:
133
- #
134
135
  # %w(a b c).to_hash( %w(1 2 3) ) # => {"a"=>"1", "b"=>"2", "c"=>"3"}
135
136
  # ["a", ["b", "c"]].to_hash( %w(1 2 3) ) # => {"a"=>"1", ["b", "c"]=>"2"}
136
137
  # ["1", "2", "3"].to_hash(["a", ["b", "c"]]) # => {"1"=>"a", "2"=>["b", "c"], "3"=>nil}
@@ -139,5 +140,65 @@ module RbExt
139
140
  def to_hash(values)
140
141
  Hash[ *(0...self.size()).inject([]) { |arr, ix| arr.push(self[ix], values[ix]) } ]
141
142
  end
143
+
144
+
145
+
146
+
147
+ ########################################################
148
+ ################## ARITHMETIC METHODS ##################
149
+ ########################################################
150
+
151
+ # Calculates the arithmetical mean of the values in the array
152
+ # [http://en.wikipedia.org/wiki/Average#Arithmetic_mean]
153
+ #
154
+ # Example:
155
+ # [1, 2, 3, 4, 5].arithmetic_mean # => 3.0
156
+ #
157
+ def arithmetic_mean
158
+ inject(0) { |sum, val| sum + val.to_f } / length.to_f
159
+ end
160
+
161
+ # Calculates the geographical mean of the values in the array
162
+ # [http://en.wikipedia.org/wiki/Average#Geometric_mean]
163
+ #
164
+ # Example:
165
+ # [2, 3, 4.5].geometric_mean # => 3.0
166
+ #
167
+ def geometric_mean
168
+ inject(1) { |product, val| product * val.to_f } ** (1.0 / length.to_f)
169
+ end
170
+
171
+ # Calculates the harmonic mean of the values in the array
172
+ # [http://en.wikipedia.org/wiki/Average#Harmonic_mean]
173
+ #
174
+ # Example:
175
+ # [1, 2, 3, 4, 5].harmonic_mean # => 2.18978102189781
176
+ #
177
+ def harmonic_mean
178
+ length.to_f / inject(0) { |sum, val| sum + (1.0 / val.to_f) }
179
+ end
180
+
181
+ # Calculates the root mean square of the values in the array
182
+ # [http://en.wikipedia.org/wiki/Quadratic_mean]
183
+ #
184
+ #
185
+ # Example:
186
+ # [10, 12, 14, 20].quadratic_mean # => 14.491376746189438
187
+ #
188
+ def quadratic_mean
189
+ (inject(0) { |sum, val| sum + (val.to_f * val.to_f) } / length.to_f) ** (1.0 / 2.0)
190
+ end
191
+
192
+ # Calculates the mid range of the values in the array
193
+ # by (max + min) / 2.0
194
+ # [http://en.wikipedia.org/wiki/Midrange]
195
+ #
196
+ # Example:
197
+ # [1, 5, 2, 3].mid_range # => 3.0
198
+ #
199
+ def mid_range
200
+ tmp = sort
201
+ (tmp.last.to_f + tmp.first.to_f) / 2.0
202
+ end
142
203
  end
143
204
  end
data/lib/rb_ext/hash.rb CHANGED
@@ -5,8 +5,8 @@ module RbExt
5
5
  # matched by the key names given to the method.
6
6
  #
7
7
  # Example:
8
- # { :a => 1, :b => 2 }.pass(:a) # => { :a => 1 }
9
- # { :abc => 1, :bbc => 2 }.pass(/^ab/) # => { :abc => 1 }
8
+ # { :a => 1, :b => 2 }.only(:a) # => { :a => 1 }
9
+ # { :abc => 1, :bbc => 2 }.only(/^ab/) # => { :abc => 1 }
10
10
  #
11
11
  def only(*keys)
12
12
  if keys.first.is_a? Regexp
@@ -20,8 +20,8 @@ module RbExt
20
20
  # that did not match the key names given to the method.
21
21
  #
22
22
  # Example:
23
- # { :a => 1, :b => 2 }.block(:a) # => { :b => 2 }
24
- # { :abc => 1, :bbc => 2 }.block(/^ab/) # => { :bbc => 2 }
23
+ # { :a => 1, :b => 2 }.except(:a) # => { :b => 2 }
24
+ # { :abc => 1, :bbc => 2 }.except(/^ab/) # => { :bbc => 2 }
25
25
  #
26
26
  def except(*keys)
27
27
  if keys.first.is_a? Regexp
data/rb_ext.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rb_ext}
8
- s.version = "0.3.1"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Robert Neumayr"]
12
- s.date = %q{2011-04-27}
12
+ s.date = %q{2011-04-28}
13
13
  s.description = %q{Adds a bunch of methods to Ruby's Core Library, like Array#split or Hash#pick}
14
14
  s.email = %q{kontakt@icatcher.at}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
8
- - 1
9
- version: 0.3.1
7
+ - 4
8
+ - 0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Robert Neumayr
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-04-27 00:00:00 +02:00
17
+ date: 2011-04-28 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -96,7 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
96
  requirements:
97
97
  - - ">="
98
98
  - !ruby/object:Gem::Version
99
- hash: -892137137
99
+ hash: -934454225
100
100
  segments:
101
101
  - 0
102
102
  version: "0"