ruby-nuggets 0.5.8 → 0.6.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/README +1 -1
- data/lib/nuggets/array/limit.rb +5 -0
- data/lib/nuggets/array/limit_mixin.rb +45 -0
- data/lib/nuggets/array/standard_deviation.rb +5 -0
- data/lib/nuggets/array/standard_deviation_mixin.rb +54 -0
- data/lib/nuggets/array/variance.rb +5 -0
- data/lib/nuggets/array/variance_mixin.rb +58 -0
- data/lib/nuggets/hash/unroll_mixin.rb +1 -1
- data/lib/nuggets/util/content_type.rb +3 -2
- data/lib/nuggets/version.rb +2 -2
- metadata +8 -2
data/README
CHANGED
@@ -0,0 +1,45 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2009 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
module Nuggets
|
29
|
+
class Array
|
30
|
+
module LimitMixin
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# array.limit(min, max) => new_array
|
34
|
+
#
|
35
|
+
# Returns a new array of all distinct values in _array_ limited to +min+
|
36
|
+
# and +max+ (cf. Numeric#limit).
|
37
|
+
def limit(min, max)
|
38
|
+
map { |item| item.between(min, max) }.uniq
|
39
|
+
end
|
40
|
+
|
41
|
+
alias_method :between, :limit
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2009 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
require 'nuggets/array/variance_mixin'
|
29
|
+
|
30
|
+
module Nuggets
|
31
|
+
class Array
|
32
|
+
module StandardDeviationMixin
|
33
|
+
|
34
|
+
def self.included(base)
|
35
|
+
base.send :include, Nuggets::Array::VarianceMixin
|
36
|
+
end
|
37
|
+
|
38
|
+
# call-seq:
|
39
|
+
# array.standard_deviation => aFloat
|
40
|
+
#
|
41
|
+
# Calculates the standard deviation of the items contained in _array_.
|
42
|
+
def standard_deviation
|
43
|
+
begin
|
44
|
+
Math.sqrt(block_given? ? variance { |*a| yield(*a) } : variance)
|
45
|
+
rescue Errno::EDOM
|
46
|
+
0.0
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
alias_method :std, :standard_deviation
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2009 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
module Nuggets
|
29
|
+
class Array
|
30
|
+
module VarianceMixin
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# array.variance => aFloat
|
34
|
+
#
|
35
|
+
# Calculates the variance of the items in _array_.
|
36
|
+
#
|
37
|
+
# Based on <http://warrenseen.com/blog/2006/03/13/how-to-calculate-standard-deviation/>.
|
38
|
+
def variance
|
39
|
+
s, mean = 0.0, 0.0
|
40
|
+
|
41
|
+
return s if empty?
|
42
|
+
|
43
|
+
each_with_index { |x, n|
|
44
|
+
x = yield x if block_given?
|
45
|
+
|
46
|
+
delta = x - mean
|
47
|
+
mean += delta / (n + 1)
|
48
|
+
s += delta * (x - mean)
|
49
|
+
}
|
50
|
+
|
51
|
+
s / size
|
52
|
+
end
|
53
|
+
|
54
|
+
alias_method :var, :variance
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -34,7 +34,7 @@ module Nuggets
|
|
34
34
|
# hash.unroll(*value_keys, :sort => ...) => anArray
|
35
35
|
# hash.unroll(*value_keys) { |value_hash| ... } => anArray
|
36
36
|
#
|
37
|
-
# "Unrolls" a nested hash, so that each path through
|
37
|
+
# "Unrolls" a nested hash, so that each path through _hash_ results in a
|
38
38
|
# row that is, e.g., suitable for use with CSV.
|
39
39
|
#
|
40
40
|
# Note that from the final hash ("value hash") only the values are used,
|
@@ -78,8 +78,9 @@ module Util
|
|
78
78
|
# NOTE: This is really only useful with the filemagic and mime-types gems
|
79
79
|
# installed.
|
80
80
|
def of(path)
|
81
|
-
File.content_type(path) || URI.content_type(path) ||
|
82
|
-
|
81
|
+
File.content_type(path) || URI.content_type(path) || (
|
82
|
+
t = MIME::Types.of(path).first and t.content_type
|
83
|
+
)
|
83
84
|
end
|
84
85
|
|
85
86
|
end
|
data/lib/nuggets/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-nuggets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jens Wille
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-07-
|
12
|
+
date: 2009-07-31 00:00:00 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -36,13 +36,19 @@ files:
|
|
36
36
|
- lib/nuggets/numeric/duration.rb
|
37
37
|
- lib/nuggets/numeric/signum.rb
|
38
38
|
- lib/nuggets/array/to_hash.rb
|
39
|
+
- lib/nuggets/array/variance.rb
|
39
40
|
- lib/nuggets/array/shuffle.rb
|
41
|
+
- lib/nuggets/array/variance_mixin.rb
|
42
|
+
- lib/nuggets/array/standard_deviation_mixin.rb
|
43
|
+
- lib/nuggets/array/limit.rb
|
40
44
|
- lib/nuggets/array/format.rb
|
41
45
|
- lib/nuggets/array/monotone.rb
|
42
46
|
- lib/nuggets/array/only.rb
|
43
47
|
- lib/nuggets/array/flatten_once.rb
|
48
|
+
- lib/nuggets/array/standard_deviation.rb
|
44
49
|
- lib/nuggets/array/rand.rb
|
45
50
|
- lib/nuggets/array/combination.rb
|
51
|
+
- lib/nuggets/array/limit_mixin.rb
|
46
52
|
- lib/nuggets/array/in_order.rb
|
47
53
|
- lib/nuggets/hash/unroll_mixin.rb
|
48
54
|
- lib/nuggets/hash/only.rb
|