utilities 0.0.12 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
data/lib/utilities.rb CHANGED
@@ -181,7 +181,7 @@ module Utilities
181
181
  # If population is set to true, then we consider the dataset as the complete population
182
182
  # Else, we consider the dataset as a sample, so we use the sample standard deviation (size - 1)
183
183
  def standard_deviation( population = false )
184
- size > 1 ? Math.sqrt( variance / ( size - ( population ? 0 : 1 ) ) ) : 0.0
184
+ size > 1 ? Math.sqrt( variance / ( size - ( population ? 0 : 1 ) ) ) : 0.0
185
185
  end
186
186
  alias_method :std_dev, :standard_deviation
187
187
 
@@ -337,3 +337,24 @@ class Array
337
337
  end
338
338
  alias_method :to_stats!, :to_stat!
339
339
  end
340
+
341
+
342
+ module Enumerable
343
+ # Collects the first N truthy values
344
+ # {}.collect_first {|a, b| obj} => obj
345
+ # {}.collect_first(n) {|a, b| obj} => [obj]
346
+ def collect_first( amount = nil, &block )
347
+ array = !amount.nil?
348
+ amount ||= 1
349
+
350
+ values = []
351
+ self.each do |val|
352
+ break if values.length >= amount
353
+ t = yield(val)
354
+ values << t if t
355
+ end
356
+
357
+ !array && amount == 1 ? values.first : values
358
+ end
359
+ alias_method :map_first, :collect_first
360
+ end
data/lib/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  class Utilities
2
2
  MAJOR = 0
3
3
  MINOR = 0
4
- BUILD = 12
4
+ BUILD = 13
5
5
 
6
6
  VERSION = "#{MAJOR}.#{MINOR}.#{BUILD}"
7
7
  end
@@ -1,4 +1,4 @@
1
- require '../lib/utilities'
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "utilities")
2
2
 
3
3
  describe Range do
4
4
  it "#intersection should returns a range containing elements common to the two ranges, with no duplicates" do
@@ -61,3 +61,26 @@ describe Numeric do
61
61
  100.percentage_of(50).should == 200
62
62
  end
63
63
  end
64
+
65
+ describe Hash do
66
+ it "#collect_keys should returns a new hash with the results of running block once for every key in self" do
67
+ {"a"=>1, "b"=>2, "c"=>3}.collect_keys{|k| k + "z"}.should == {"az"=>1, "bz"=>2, "cz"=>3}
68
+ end
69
+
70
+ it "#collect_values should returns a new hash with the results of running block once for every value in self" do
71
+ {"a"=>1, "b"=>2, "c"=>3}.collect_values{|v| v**2}.should == {"a"=>1, "b"=>4, "c"=>9}
72
+ end
73
+
74
+ it "#symbolize_keys should returns a new hash where all keys have been symbolized" do
75
+ {"a"=>1, "b"=>2, "c"=>3}.symbolize_keys.should == {:a=>1, :b=>2, :c=>3}
76
+ end
77
+ end
78
+
79
+ describe Enumerable do
80
+ it "#collect_first should return the first collected elements" do
81
+ h = {:a => 1, :b => 2, :c => 3}
82
+ h.collect_first{|k, v| v if v == 2}.should == 2
83
+ h.collect_first(1){|k, v| v}.should == [1]
84
+ h.collect_first(2){|k, v| v}.should == [1, 2]
85
+ end
86
+ end
metadata CHANGED
@@ -1,74 +1,56 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: utilities
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 12
9
- version: 0.0.12
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.13
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Christian Blais
13
9
  - Guillaume Malette
14
10
  - Louis-Mathieu Houle
15
11
  autorequire:
16
12
  bindir: bin
17
13
  cert_chain: []
18
-
19
- date: 2011-08-21 00:00:00 -04:00
20
- default_executable:
14
+ date: 2011-08-22 00:00:00.000000000Z
21
15
  dependencies: []
22
-
23
- description: Few utilities include in all my projects, including a module for statistics, some to_date and to_time functions as well as intersection method for Range object.
24
- email:
16
+ description: Few utilities include in all my projects, including a module for statistics,
17
+ some to_date and to_time functions as well as intersection method for Range object.
18
+ email:
25
19
  - christ.blais@gmail.com
26
20
  - gmalette@gmail.com
27
21
  executables: []
28
-
29
22
  extensions: []
30
-
31
23
  extra_rdoc_files: []
32
-
33
- files:
24
+ files:
34
25
  - LICENSE
35
26
  - README
36
27
  - utilities.gemspec
37
28
  - lib/utilities.rb
38
29
  - lib/version.rb
39
30
  - test/test_utilities.rb
40
- has_rdoc: true
41
31
  homepage: http://github.com/christianblais/utilities
42
32
  licenses: []
43
-
44
33
  post_install_message:
45
34
  rdoc_options: []
46
-
47
- require_paths:
35
+ require_paths:
48
36
  - lib
49
37
  - test
50
- required_ruby_version: !ruby/object:Gem::Requirement
38
+ required_ruby_version: !ruby/object:Gem::Requirement
51
39
  none: false
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- segments:
56
- - 0
57
- version: "0"
58
- required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
45
  none: false
60
- requirements:
61
- - - ">="
62
- - !ruby/object:Gem::Version
63
- segments:
64
- - 0
65
- version: "0"
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
66
50
  requirements: []
67
-
68
51
  rubyforge_project:
69
- rubygems_version: 1.3.7
52
+ rubygems_version: 1.8.6
70
53
  signing_key:
71
54
  specification_version: 3
72
55
  summary: Few utilities include in all my projects
73
56
  test_files: []
74
-