zucker 100.0.0 → 100.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/zucker.rb CHANGED
@@ -1,13 +1,11 @@
1
1
  begin
2
2
  require 'refine'
3
3
  rescue LoadError
4
- if RUBY_VERSION < '2.0'
5
- warn "The zucker library depends on refinements, please install the refine gem, when running a RubyVersion < 2.0"
6
- end
7
4
  end
8
5
 
6
+
9
7
  module Zucker
10
- VERSION = '100.0.0'
8
+ VERSION = '100.1.0'
11
9
  DATE = '2014-04-01'
12
10
 
13
11
  class << self
@@ -0,0 +1,31 @@
1
+ require "zucker"
2
+
3
+ module Zucker
4
+ module ArrayStats
5
+ refine Array do
6
+ def mean
7
+ inject(&:+) / length.to_f
8
+ end
9
+
10
+ def stdev(type = :population)
11
+ case type
12
+ when :population then stdev_population
13
+ when :sample then stdev_sample
14
+ else raise ArgumentError.new("%s is not a valid argument" % type)
15
+ end
16
+ end
17
+
18
+ def stdev_sample
19
+ Math.sqrt(inject(0.0) { |sum, x| sum + (mean - x) ** 2 } / (length - 1))
20
+ end
21
+
22
+ def stdev_population
23
+ Math.sqrt(inject(0.0) { |sum, x| sum + (mean - x) ** 2 } / length)
24
+ end
25
+
26
+ def z_score(type = :population)
27
+ map { |x| (x - mean) / stdev(type) }
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,11 @@
1
+ require "zucker"
2
+
3
+ module Zucker
4
+ module ChainMap
5
+ refine Array do
6
+ def chain_map(*args)
7
+ args.inject(self) { |collection, action| collection.map(&action) }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'zucker'
2
+
3
+ module Zucker
4
+ module FloatExtras
5
+ refine Float do
6
+ def same?(other)
7
+ (self - other).abs < Float::EPSILON
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ require "zucker"
2
+
3
+ module Zucker
4
+ module InnerMap
5
+ refine Array do
6
+ def inner_map(&block)
7
+ map { |object| object.map(&block) }
8
+ end
9
+
10
+ def inner_inject(default = :not_used, &block)
11
+ map { |object| default == :not_used ? object.inject(&block) : object.inject(default, &block) }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,82 @@
1
+ require "zucker/array_stats"
2
+ using Zucker::ArrayStats
3
+
4
+ describe "doing statistics on arrays" do
5
+ let(:list) { (2..5).to_a }
6
+
7
+ describe "Array#mean" do
8
+ it "should return the average" do
9
+ list.mean.should == 3.5
10
+ end
11
+ end
12
+
13
+ describe "Array#stdev_sample" do
14
+ it "should return the standard deviation of the sample" do
15
+ list.stdev_sample.should be_close(1.2909944487358056, 1e-8)
16
+ end
17
+ end
18
+
19
+ describe "Array#stdev_population" do
20
+ it "should return the standard deviation of the population" do
21
+ list.stdev_population.should be_close(1.118033988749895, 1e-8)
22
+ end
23
+ end
24
+
25
+ describe "Array#stdev" do
26
+ it "should default to population" do
27
+ list.stdev.should be_close(list.stdev_population, 1e-8)
28
+ end
29
+
30
+ it "should delegate sample correctly" do
31
+ list.stdev(:sample).should be_close(list.stdev_sample, 1e-8)
32
+ end
33
+
34
+ it "should delegate population correctly" do
35
+ list.stdev(:population).should be_close(list.stdev_population, 1e-8)
36
+ end
37
+
38
+ it "should raise an error with any other key" do
39
+ expect { list.stdev(:pony) }.to raise_error(ArgumentError)
40
+ end
41
+ end
42
+
43
+ describe "Array#z_score" do
44
+ it "should default to population" do
45
+ list.z_score.zip(list.z_score(:population)).each do |value, actual|
46
+ value.should be_close(actual, 1e-8)
47
+ end
48
+ end
49
+
50
+ it "should delegate sample correctly" do
51
+ sample_z_score = [
52
+ -1.161895003862225,
53
+ -0.3872983346207417,
54
+ 0.3872983346207417,
55
+ 1.161895003862225
56
+ ]
57
+
58
+ p list
59
+
60
+ list.z_score(:sample).zip(sample_z_score).each do |value, actual|
61
+ value.should be_close(actual, 1e-8)
62
+ end
63
+ end
64
+
65
+ it "should delegate population correctly" do
66
+ population_z_score = [
67
+ -1.3416407864998738,
68
+ -0.4472135954999579,
69
+ 0.4472135954999579,
70
+ 1.3416407864998738
71
+ ]
72
+
73
+ list.z_score(:population).zip(population_z_score).each do |value, actual|
74
+ value.should be_close(actual, 1e-8)
75
+ end
76
+ end
77
+
78
+ it "should raise an error with any other key" do
79
+ expect { list.z_score(:pony) }.to raise_error(ArgumentError)
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,16 @@
1
+ require "zucker/chain_map"
2
+ using Zucker::ChainMap
3
+
4
+ describe "Array#chain_map" do
5
+ it "should repeatedly chain symbols as map calls" do
6
+ list = [-3, -2, -1]
7
+
8
+ list.chain_map(:abs, :to_s) == %w{3 2 1}
9
+ end
10
+
11
+ it "should repeatedly chain blocks as map calls" do
12
+ list = [-3, -2, -1]
13
+
14
+ list.chain_map(->(e) { e ** 2 }, ->(e) { e * -1 }) == [-9, -4, -1]
15
+ end
16
+ end
@@ -13,7 +13,7 @@ describe File do
13
13
  describe '.gsub' do
14
14
  after{ File.delete! 'random_filename' }
15
15
  let :random_filename do
16
- 'test_' + (0..20).map{|e| [*'a'..'z'].send RUBY_VERSION > '1.9' ? :sample : :choice }*''
16
+ 'test_' + (0..20).map{|e| [*'a'..'z'].sample }*''
17
17
  end
18
18
 
19
19
  it 'should read filename in arg1, substitute every key in the arg2 with its value and save the file' do
@@ -34,7 +34,7 @@ describe File do
34
34
  describe '.delete!' do
35
35
  after{ File.delete! 'random_filename' }
36
36
  let :random_filename do
37
- 'test_' + (0..20).map{|e| [*'a'..'z'].send RUBY_VERSION > '1.9' ? :sample : :choice }*''
37
+ 'test_' + (0..20).map{|e| [*'a'..'z'].sample }*''
38
38
  end
39
39
 
40
40
  it 'should delete the filename given as argument if it exists + return non-nil' do
@@ -43,7 +43,7 @@ describe File do
43
43
  proc do
44
44
  res = File.delete! random_filename
45
45
  end.should_not raise_exception
46
- res.should be_true
46
+ res.should be_truthy
47
47
  end
48
48
 
49
49
  it 'should do nothing if the filename given as argument does not exist + return nil' do
@@ -51,7 +51,7 @@ describe File do
51
51
  proc do
52
52
  res = File.delete! random_filename
53
53
  end.should_not raise_exception
54
- res.should be_false
54
+ res.should be_falsey
55
55
  end
56
56
  end
57
57
  end
@@ -0,0 +1,10 @@
1
+ require 'zucker/float_extras'
2
+ using Zucker::FloatExtras
3
+
4
+ describe Float do
5
+ describe '#same?' do
6
+ it 'returns true if other float represents the same number' do
7
+ expect( 0.3.same?(0.1 + 0.2) ).to be true
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,24 @@
1
+ require "zucker/inner_map"
2
+ using Zucker::InnerMap
3
+
4
+ describe "Array#inner_map" do
5
+ it "should delegate map to inner lists" do
6
+ list = [[1, 2], [3, 4]]
7
+
8
+ list.inner_map { |i| i + 1 }.should == [[2, 3], [4, 5]]
9
+ end
10
+ end
11
+
12
+ describe "Array#inner_inject" do
13
+ it "should delegate inject to inner lists" do
14
+ list = [%w{a b c}, %w{d e f}]
15
+
16
+ list.inner_inject(&:+).should == list.map(&:join)
17
+ end
18
+
19
+ it "should take default values" do
20
+ list = [[3, 2, 1], [-4]]
21
+
22
+ list.inner_inject(4, &:+).should == [10, 0]
23
+ end
24
+ end
data/spec/iterate_spec.rb CHANGED
@@ -55,7 +55,7 @@ describe 'Object#iterate' do
55
55
  res[:iter_b_a] = [] # ....
56
56
 
57
57
  enumerator = iterate a,b
58
- enumerator.should be_kind_of(RUBY_VERSION < '1.9' ? Enumerable::Enumerator : Enumerator)
58
+ enumerator.should be_kind_of(Enumerator)
59
59
  enumerator.to_a.should == [[1,'a'], [2,'b'], [3,'c'], [nil, 'd']]
60
60
  end
61
61
  end
data/zucker.gemspec CHANGED
@@ -4,17 +4,18 @@ require File.dirname(__FILE__) + '/lib/zucker'
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "zucker"
6
6
  s.version = Zucker::VERSION
7
- s.authors = ['Jan Lelis','others']
7
+ s.authors = ['Jan Lelis']
8
8
  s.email = 'mail@janlelis.de'
9
9
  s.summary = "Sweetens your Ruby code with syntactic sugar."
10
- s.description = "Lots of refinements to sweeten your Ruby code"
10
+ s.description = "Lots of refinements to sweeten your Ruby code."
11
11
  s.homepage = "http://rubyzucker.info"
12
- s.files = Dir.glob( %w[{lib,spec}/**/*.rb desc/**/*.yaml] ) + %w{Rakefile zucker.gemspec MIT-LICENSE README.md CHANGELOG doc/create_documentation.rb doc/zucker.html}
12
+ s.files = Dir.glob( %w[{lib,spec}/**/*.rb desc/**/*.yaml] ) + %w{Rakefile zucker.gemspec MIT-LICENSE.txt README.md CHANGELOG.txt doc/create_documentation.rb doc/zucker.html}
13
13
  s.require_paths = ["lib"]
14
- s.required_ruby_version = '>= 1.9.2'
15
14
  s.license = 'MIT'
15
+
16
+ s.required_ruby_version = '~> 2.0'
16
17
  s.add_dependency 'refine', '~> 0'
17
- s.add_development_dependency 'rake', '~> 0'
18
- s.add_development_dependency 'rspec', '~> 2.14'
19
- s.add_development_dependency 'coderay', '~> 0'
18
+ s.add_development_dependency 'rake', '~> 10.4'
19
+ s.add_development_dependency 'rspec', '~> 2.99'
20
+ s.add_development_dependency 'coderay', '~> 1.1'
20
21
  end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zucker
3
3
  version: !ruby/object:Gem::Version
4
- version: 100.0.0
4
+ version: 100.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Lelis
8
- - others
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-04-01 00:00:00.000000000 Z
11
+ date: 2015-04-01 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: refine
@@ -31,50 +30,50 @@ dependencies:
31
30
  requirements:
32
31
  - - "~>"
33
32
  - !ruby/object:Gem::Version
34
- version: '0'
33
+ version: '10.4'
35
34
  type: :development
36
35
  prerelease: false
37
36
  version_requirements: !ruby/object:Gem::Requirement
38
37
  requirements:
39
38
  - - "~>"
40
39
  - !ruby/object:Gem::Version
41
- version: '0'
40
+ version: '10.4'
42
41
  - !ruby/object:Gem::Dependency
43
42
  name: rspec
44
43
  requirement: !ruby/object:Gem::Requirement
45
44
  requirements:
46
45
  - - "~>"
47
46
  - !ruby/object:Gem::Version
48
- version: '2.14'
47
+ version: '2.99'
49
48
  type: :development
50
49
  prerelease: false
51
50
  version_requirements: !ruby/object:Gem::Requirement
52
51
  requirements:
53
52
  - - "~>"
54
53
  - !ruby/object:Gem::Version
55
- version: '2.14'
54
+ version: '2.99'
56
55
  - !ruby/object:Gem::Dependency
57
56
  name: coderay
58
57
  requirement: !ruby/object:Gem::Requirement
59
58
  requirements:
60
59
  - - "~>"
61
60
  - !ruby/object:Gem::Version
62
- version: '0'
61
+ version: '1.1'
63
62
  type: :development
64
63
  prerelease: false
65
64
  version_requirements: !ruby/object:Gem::Requirement
66
65
  requirements:
67
66
  - - "~>"
68
67
  - !ruby/object:Gem::Version
69
- version: '0'
70
- description: Lots of refinements to sweeten your Ruby code
68
+ version: '1.1'
69
+ description: Lots of refinements to sweeten your Ruby code.
71
70
  email: mail@janlelis.de
72
71
  executables: []
73
72
  extensions: []
74
73
  extra_rdoc_files: []
75
74
  files:
76
- - CHANGELOG
77
- - MIT-LICENSE
75
+ - CHANGELOG.txt
76
+ - MIT-LICENSE.txt
78
77
  - README.md
79
78
  - Rakefile
80
79
  - doc/create_documentation.rb
@@ -83,14 +82,18 @@ files:
83
82
  - lib/zucker/alias_for.rb
84
83
  - lib/zucker/all.rb
85
84
  - lib/zucker/array_op.rb
85
+ - lib/zucker/array_stats.rb
86
86
  - lib/zucker/blank.rb
87
87
  - lib/zucker/camel_snake.rb
88
+ - lib/zucker/chain_map.rb
88
89
  - lib/zucker/constantize.rb
89
90
  - lib/zucker/dir_extras.rb
90
91
  - lib/zucker/egonil.rb
91
92
  - lib/zucker/file_extras.rb
93
+ - lib/zucker/float_extras.rb
92
94
  - lib/zucker/hash_extras.rb
93
95
  - lib/zucker/hash_op.rb
96
+ - lib/zucker/inner_map.rb
94
97
  - lib/zucker/instance_variables_from.rb
95
98
  - lib/zucker/iterate.rb
96
99
  - lib/zucker/marshal_copy.rb
@@ -104,14 +107,18 @@ files:
104
107
  - lib/zucker/unary_conversion.rb
105
108
  - spec/alias_for_spec.rb
106
109
  - spec/array_op_spec.rb
110
+ - spec/array_stats_spec.rb
107
111
  - spec/blank_spec.rb
108
112
  - spec/camel_snake_spec.rb
113
+ - spec/chain_map_spec.rb
109
114
  - spec/constantize_spec.rb
110
115
  - spec/dir_extras_spec.rb
111
116
  - spec/egonil_spec.rb
112
117
  - spec/file_extras_spec.rb
118
+ - spec/float_extras_spec.rb
113
119
  - spec/hash_extras_spec.rb
114
120
  - spec/hash_op_spec.rb
121
+ - spec/inner_map_spec.rb
115
122
  - spec/instance_variables_from_spec.rb
116
123
  - spec/iterate_spec.rb
117
124
  - spec/marshal_copy_spec.rb
@@ -135,9 +142,9 @@ require_paths:
135
142
  - lib
136
143
  required_ruby_version: !ruby/object:Gem::Requirement
137
144
  requirements:
138
- - - ">="
145
+ - - "~>"
139
146
  - !ruby/object:Gem::Version
140
- version: 1.9.2
147
+ version: '2.0'
141
148
  required_rubygems_version: !ruby/object:Gem::Requirement
142
149
  requirements:
143
150
  - - ">="
@@ -145,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
152
  version: '0'
146
153
  requirements: []
147
154
  rubyforge_project:
148
- rubygems_version: 2.2.2
155
+ rubygems_version: 2.4.6
149
156
  signing_key:
150
157
  specification_version: 4
151
158
  summary: Sweetens your Ruby code with syntactic sugar.