hash-utils 0.15.0 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,12 +1,12 @@
1
1
  source "http://rubygems.org"
2
2
  # Add dependencies required to use your gem here.
3
3
  # Example:
4
- # gem "multitype-introspection", ">= 0.1.0"
4
+ gem "ruby-version", ">= 0.1.0"
5
5
 
6
6
  # Add dependencies to develop your gem here.
7
7
  # Include everything needed to run rake, tests, features, etc.
8
8
  group :development do
9
- gem "bundler", "~> 1.0.0"
10
- gem "jeweler", "~> 1.5.2"
9
+ gem "bundler", ">= 1.0.0"
10
+ gem "jeweler", ">= 1.5.2"
11
11
  gem "riot", ">= 0.12.3"
12
12
  end
data/Gemfile.lock CHANGED
@@ -2,19 +2,23 @@ GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
4
  git (1.2.5)
5
- jeweler (1.5.2)
6
- bundler (~> 1.0.0)
5
+ hash-utils (0.16.0)
6
+ jeweler (1.6.2)
7
+ bundler (~> 1.0)
7
8
  git (>= 1.2.5)
8
9
  rake
9
- rake (0.9.0)
10
- riot (0.12.3)
10
+ rake (0.9.2)
11
+ riot (0.12.4)
11
12
  rr
12
13
  rr (1.0.2)
14
+ ruby-version (0.1.0)
15
+ hash-utils (>= 0.16.0)
13
16
 
14
17
  PLATFORMS
15
18
  ruby
16
19
 
17
20
  DEPENDENCIES
18
- bundler (~> 1.0.0)
19
- jeweler (~> 1.5.2)
21
+ bundler (>= 1.0.0)
22
+ jeweler (>= 1.5.2)
20
23
  riot (>= 0.12.3)
24
+ ruby-version (>= 0.1.0)
data/README.md CHANGED
@@ -1,18 +1,19 @@
1
1
  Hash Utils
2
2
  ==========
3
3
 
4
- **hash-utils** adds more than 95 useful and frequently rather
4
+ **hash-utils** adds more than 105 useful and frequently rather
5
5
  fundamental methods which are missing in Ruby programming language,
6
6
  to several core classes. It tries to be similar project to
7
7
  [Ruby Facets][1] on principle, but less complex, more practical,
8
8
  non-atomic and organized by better way.
9
9
 
10
- - `Array` – 10 methods,
10
+ - `Array` – 12 methods,
11
11
  - `File` – 2 methods,
12
- - `Hash` – 36 methods,
12
+ - `Hash` – 38 methods,
13
13
  - `IO` – 1 method,
14
14
  - `Module` – 1 method,
15
- - `Object` – 6 methods,
15
+ - `Numeric` – 4 method,
16
+ - `Object` – 8 methods,
16
17
  - `String` – 34 methods,
17
18
  - `StringIO` – 1 method,
18
19
  - `Symbol` – 7 methods.
data/Rakefile CHANGED
@@ -1,6 +1,7 @@
1
1
  # encoding: utf-8
2
2
  require 'rubygems'
3
3
  require 'bundler'
4
+
4
5
  begin
5
6
  Bundler.setup(:default, :development)
6
7
  rescue Bundler::BundlerError => e
@@ -8,15 +9,16 @@ rescue Bundler::BundlerError => e
8
9
  $stderr.puts "Run `bundle install` to install missing gems"
9
10
  exit e.status_code
10
11
  end
11
- require 'rake'
12
12
 
13
+ require 'rake'
13
14
  require 'jeweler'
15
+
14
16
  Jeweler::Tasks.new do |gem|
15
17
  # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
16
18
  gem.name = "hash-utils"
17
19
  gem.homepage = "http://github.com/martinkozak/hash-utils"
18
20
  gem.license = "MIT"
19
- gem.summary = 'Adds more than 95 useful and frequently rather fundamental methods which are missing in Ruby programming language, to Array, File, Hash, Module, Object, String and Symbol classes. It tries to be similar project to Ruby Facets on principle, but less complex, more practical, non-atomic and organized by better way.'
21
+ gem.summary = 'Adds more than 105 useful and frequently rather fundamental methods which are missing in Ruby programming language, to Array, File, Hash, Module, Object, String and Symbol classes. It tries to be similar project to Ruby Facets on principle, but less complex, more practical, non-atomic and organized by better way.'
20
22
  gem.post_install_message = "\nHASH UTILS: Be warn, Hash#all? has been removed since version 0.15.0 because of\nconflict with built-in one with in fact equivalent functionallity. Please, check\nyour code if you can and switch to Ruby's one.\n\n"
21
23
  gem.email = "martinkozak@martinkozak.net"
22
24
  gem.authors = ["Martin Kozák"]
@@ -26,13 +28,3 @@ Jeweler::Tasks.new do |gem|
26
28
  # gem.add_development_dependency 'rspec', '> 1.2.3'
27
29
  end
28
30
  Jeweler::RubygemsDotOrgTasks.new
29
-
30
- require 'rake/rdoctask'
31
- Rake::RDocTask.new do |rdoc|
32
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
33
-
34
- rdoc.rdoc_dir = 'rdoc'
35
- rdoc.title = "hash-utils #{version}"
36
- rdoc.rdoc_files.include('README*')
37
- rdoc.rdoc_files.include('lib/**/*.rb')
38
- end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.15.0
1
+ 0.16.0
data/hash-utils.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{hash-utils}
8
- s.version = "0.15.0"
8
+ s.version = "0.16.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Martin Kozák}]
12
- s.date = %q{2011-05-29}
12
+ s.date = %q{2011-06-23}
13
13
  s.email = %q{martinkozak@martinkozak.net}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE.txt",
@@ -47,24 +47,27 @@ your code if you can and switch to Ruby's one.
47
47
 
48
48
  }
49
49
  s.require_paths = [%q{lib}]
50
- s.rubygems_version = %q{1.8.4}
51
- s.summary = %q{Adds more than 95 useful and frequently rather fundamental methods which are missing in Ruby programming language, to Array, File, Hash, Module, Object, String and Symbol classes. It tries to be similar project to Ruby Facets on principle, but less complex, more practical, non-atomic and organized by better way.}
50
+ s.rubygems_version = %q{1.8.5}
51
+ s.summary = %q{Adds more than 105 useful and frequently rather fundamental methods which are missing in Ruby programming language, to Array, File, Hash, Module, Object, String and Symbol classes. It tries to be similar project to Ruby Facets on principle, but less complex, more practical, non-atomic and organized by better way.}
52
52
 
53
53
  if s.respond_to? :specification_version then
54
54
  s.specification_version = 3
55
55
 
56
56
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
57
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
58
- s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
57
+ s.add_runtime_dependency(%q<ruby-version>, [">= 0.1.0"])
58
+ s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
59
+ s.add_development_dependency(%q<jeweler>, [">= 1.5.2"])
59
60
  s.add_development_dependency(%q<riot>, [">= 0.12.3"])
60
61
  else
61
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
62
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
62
+ s.add_dependency(%q<ruby-version>, [">= 0.1.0"])
63
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
64
+ s.add_dependency(%q<jeweler>, [">= 1.5.2"])
63
65
  s.add_dependency(%q<riot>, [">= 0.12.3"])
64
66
  end
65
67
  else
66
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
67
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
68
+ s.add_dependency(%q<ruby-version>, [">= 0.1.0"])
69
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
70
+ s.add_dependency(%q<jeweler>, [">= 1.5.2"])
68
71
  s.add_dependency(%q<riot>, [">= 0.12.3"])
69
72
  end
70
73
  end
data/lib/hash-utils.rb CHANGED
@@ -6,6 +6,7 @@ require "hash-utils/file"
6
6
  require "hash-utils/hash"
7
7
  require "hash-utils/io"
8
8
  require "hash-utils/module"
9
+ require "hash-utils/numeric"
9
10
  require "hash-utils/object"
10
11
  require "hash-utils/string"
11
12
  require "hash-utils/stringio"
@@ -1,6 +1,8 @@
1
1
  # encoding: utf-8
2
2
  # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
3
 
4
+ require "ruby-version"
5
+
4
6
  ##
5
7
  # Array extension.
6
8
  #
@@ -171,4 +173,52 @@ class Array
171
173
  self[7]
172
174
  end
173
175
 
176
+ ##
177
+ # Returns sum of items in the array. If other than numeric type found,
178
+ # it will be normally conacenated if it's possible.
179
+ #
180
+ # @example
181
+ # array = [1, 2, 3]
182
+ # array.sum # will return 6
183
+ # array = ["a", "b", "c"]
184
+ # array.sum # will return "abc"
185
+ #
186
+ # @return [Object] result of summing
187
+ # @since 0.16.0
188
+ #
189
+
190
+ def sum
191
+ if RUBY_VERSION[0..2] == "1.9" or Ruby::Version >= [1, 8, 7]
192
+ self.inject(:+)
193
+ else
194
+ first = true
195
+ self.inject(self.first) do |sum, i|
196
+ if first
197
+ first = false
198
+ sum
199
+ else
200
+ sum += i
201
+ end
202
+ end
203
+ end
204
+ end
205
+
206
+ ##
207
+ # Returns average of items in the array. Numeric types are expected,
208
+ # but content of the array isn't checked.
209
+ #
210
+ # @example
211
+ # array = [1, 2, 3]
212
+ # array.avg # will return 2
213
+ #
214
+ # @return [Numeric] result of averaging
215
+ # @since 0.16.0
216
+ #
217
+
218
+ def avg
219
+ self.sum.to_f / self.length
220
+ end
221
+
222
+ alias :average :avg
223
+
174
224
  end
@@ -694,4 +694,42 @@ class Hash
694
694
  return result
695
695
  end
696
696
 
697
+ ##
698
+ # Returns sum of items in the hash. If other than numeric type found,
699
+ # it will be normally conacenated if it's possible.
700
+ #
701
+ # @example
702
+ # hash = {:a => 1, :b => 2, :c => 3}
703
+ # hash.sum # will return 6
704
+ # hash = {:a => "a", :b => "b", :c => "c"}
705
+ # hash.sum # will return "abc"
706
+ #
707
+ # @return [Integer, Object] result of summing
708
+ # @see Array#sum
709
+ # @since 0.16.0
710
+ #
711
+
712
+ def sum
713
+ self.values.sum
714
+ end
715
+
716
+ ##
717
+ # Returns average of items in the hash. Numeric types are expected,
718
+ # but content of the hash isn't checked.
719
+ #
720
+ # @example
721
+ # hash = {:a => 1, :b => 2, :c => 3}
722
+ # hash.avg # will return 2
723
+ #
724
+ # @return [Numeric] result of averaging
725
+ # @since 0.16.0
726
+ #
727
+
728
+ def avg
729
+ self.values.avg
730
+ end
731
+
732
+ alias :average :avg
733
+
734
+
697
735
  end
@@ -2,3 +2,89 @@
2
2
  # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
3
 
4
4
  require "hash-utils/object"
5
+
6
+ ##
7
+ # Numeric extension.
8
+ # @since 0.16.0
9
+ #
10
+
11
+ class Numeric
12
+
13
+ ##
14
+ # Compares numbers. Returns 1 if this is higher, 0 if they are the
15
+ # same and -1 if it's lower.
16
+ #
17
+ # @param [Numeric] number number for comparsion
18
+ # @return [Integer] comparsion result, see above
19
+ # @since 0.16.0
20
+ #
21
+
22
+ def compare(number)
23
+ comp = self - number
24
+
25
+ if comp.zero?
26
+ 0
27
+ elsif comp.positive?
28
+ 1
29
+ else
30
+ -1
31
+ end
32
+ end
33
+
34
+ ##
35
+ # Indicates, number is positive, so higher than 0.
36
+ #
37
+ # @return [Boolean] +true+ it it is
38
+ # @since 0.16.0
39
+ #
40
+
41
+ def positive?
42
+ self > 0
43
+ end
44
+
45
+ alias :positive :"positive?"
46
+
47
+ ##
48
+ # Indicates, number is negative, so lower than 0.
49
+ #
50
+ # @return [Boolean] +true+ it it is
51
+ # @since 0.16.0
52
+ #
53
+
54
+ def negative?
55
+ self < 0
56
+ end
57
+
58
+ alias :negative :"negative?"
59
+
60
+ ##
61
+ # Sets the positivity to +false+, so negates the number. +0+ will
62
+ # be kept.
63
+ #
64
+ # @example
65
+ # 1.negative! # will return -1
66
+ #
67
+ # @return [Numeric] negative number
68
+ # @since 0.16.0
69
+ #
70
+
71
+ def negative!
72
+ -self.positive!
73
+ end
74
+
75
+ alias :"negate!" :"negative!"
76
+
77
+ ##
78
+ # Sets the positivity to +true+, so makes number positive. +0+ will
79
+ # be kept.
80
+ #
81
+ # @example
82
+ # -1.positive! # will return 1
83
+ #
84
+ # @return [Numeric] positive number
85
+ # @since 0.16.0
86
+ #
87
+
88
+ alias :"positive!" :abs
89
+
90
+ end
@@ -6,6 +6,55 @@
6
6
  #
7
7
 
8
8
  class Object
9
+
10
+ ##
11
+ # Returns +true+ if object is an instance of the given classes.
12
+ #
13
+ # @see #kind_of_any?
14
+ # @param [Array] classes array of class objects
15
+ # @return [Boolean] +true+ if it is, +false+ in otherwise
16
+ # @since 0.16.0
17
+ #
18
+
19
+ def instance_of_any?(classes)
20
+ if not classes.kind_of? Array
21
+ raise Exception::new("Array expected.")
22
+ end
23
+
24
+ classes.each do |cls|
25
+ if self.instance_of? cls
26
+ return true
27
+ end
28
+ end
29
+
30
+ return false
31
+ end
32
+
33
+ ##
34
+ # Returns +true+ if one of classes are the class of object,
35
+ # or if one of classes are one of the superclasses of object or
36
+ # modules included in object.
37
+ #
38
+ # @param [Array] classes array of class objects
39
+ # @return [Boolean] +true+ if it is, +false+ in otherwise
40
+ # @since 0.16.0
41
+ #
42
+
43
+ def kind_of_any?(classes)
44
+ if not classes.kind_of? Array
45
+ raise Exception::new("Array expected.")
46
+ end
47
+
48
+ classes.each do |cls|
49
+ if self.kind_of? cls
50
+ return true
51
+ end
52
+ end
53
+
54
+ return false
55
+ end
56
+
57
+ alias :"is_a_any?" :"kind_of_any?"
9
58
 
10
59
  ##
11
60
  # Indicates object is in some object which supports +#include?+.
@@ -25,7 +25,7 @@ class Symbol
25
25
  # Returns a new +Symbol+ containing +other_str+ concatenated
26
26
  # to +str+.
27
27
  #
28
- # @param [Integer] other_str string for concatenating to
28
+ # @param [Symbol, String] other_str string or symbol for concatenating to
29
29
  # @return [Symbol] new symbol
30
30
  # @since 0.12.0
31
31
  #
data/test CHANGED
@@ -8,14 +8,14 @@ require "hash-utils"
8
8
  require "riot"
9
9
 
10
10
 
11
- ## FILE
11
+ ## ARRAY
12
12
 
13
13
  # Filozofové mají Kanta rádi. Matematici mají Kanta také rádi, jen si
14
14
  # myslí, že většina z toho, co vymyslel, je blbost.
15
15
  #
16
16
  # doc. Jiří Souček
17
17
 
18
- context "File" do
18
+ context "Array" do
19
19
  asserts("#second") do
20
20
  [1, 2, 3, 4, 5, 6, 7, 8].second == 2
21
21
  end
@@ -37,8 +37,16 @@ context "File" do
37
37
  asserts("#eighth") do
38
38
  [1, 2, 3, 4, 5, 6, 7, 8].eighth == 8
39
39
  end
40
+ asserts("#avg") do
41
+ [1, 2, 3].avg == 2
42
+ end
43
+ asserts("#sum") do
44
+ [1, 2, 3].sum == 6
45
+ end
40
46
  end
41
47
 
48
+ ## FILE
49
+
42
50
  context "File" do
43
51
  asserts("#touch") do
44
52
  File.touch("./~test1")
@@ -61,7 +69,10 @@ context "Hash" do
61
69
  setup do
62
70
  { :a => 1, :b => 2 }
63
71
  end
64
-
72
+
73
+ asserts("#avg") do
74
+ topic.avg == 1.5
75
+ end
65
76
  asserts("#compact") do
66
77
  t = { :a => 1, :b => nil }
67
78
  t = t.compact
@@ -114,6 +125,9 @@ context "Hash" do
114
125
  h.keys_to_sym!
115
126
  h == {:a => "b", 2 => "c", :d => "e"}
116
127
  end
128
+ asserts("#sum") do
129
+ topic.sum == 3
130
+ end
117
131
  asserts("#take_pairs") do
118
132
  h = { :a => 1, :b => 2, :c => 3 }
119
133
  result = [ ]
@@ -154,6 +168,26 @@ context "Module" do
154
168
  end
155
169
  end
156
170
 
171
+ ## NUMERIC
172
+
173
+ context "Numeric" do
174
+ asserts("#compare") do
175
+ 5.compare(1) == 1 and 5.compare(5) == 0 and 5.compare(7) == -1
176
+ end
177
+ asserts("#negative?") do
178
+ not 5.negative? and -2.negative?
179
+ end
180
+ asserts("#negative!") do
181
+ 5.negative! == -5 and -5.negative! == -5
182
+ end
183
+ asserts("#positive?") do
184
+ 5.positive? and not -2.positive?
185
+ end
186
+ asserts("#positive!") do
187
+ 5.positive! == 5 and -5.positive! == 5
188
+ end
189
+ end
190
+
157
191
  ## OBJECT
158
192
 
159
193
  context "Object" do
@@ -166,9 +200,15 @@ context "Object" do
166
200
  asserts("#in?") do
167
201
  5.in? 1..7
168
202
  end
203
+ asserts("#instance_of_any?") do
204
+ "".instance_of_any? [String, Symbol] and not "".instance_of_any? [Integer, Float]
205
+ end
169
206
  asserts("#io?") do
170
207
  not "".io?
171
208
  end
209
+ asserts("#kind_of_any?") do
210
+ "".kind_of_any? [String, Symbol] and not "".kind_of_any? [Integer, Float]
211
+ end
172
212
  asserts("#to_b") do
173
213
  (nil.to_b === false) and ("ab".to_b === true)
174
214
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: hash-utils
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.15.0
5
+ version: 0.16.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Martin Koz\xC3\xA1k"
@@ -10,33 +10,44 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-29 00:00:00 Z
13
+ date: 2011-06-23 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: bundler
16
+ name: ruby-version
17
17
  requirement: &id001 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
- - - ~>
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.1.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: bundler
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ">="
21
32
  - !ruby/object:Gem::Version
22
33
  version: 1.0.0
23
34
  type: :development
24
35
  prerelease: false
25
- version_requirements: *id001
36
+ version_requirements: *id002
26
37
  - !ruby/object:Gem::Dependency
27
38
  name: jeweler
28
- requirement: &id002 !ruby/object:Gem::Requirement
39
+ requirement: &id003 !ruby/object:Gem::Requirement
29
40
  none: false
30
41
  requirements:
31
- - - ~>
42
+ - - ">="
32
43
  - !ruby/object:Gem::Version
33
44
  version: 1.5.2
34
45
  type: :development
35
46
  prerelease: false
36
- version_requirements: *id002
47
+ version_requirements: *id003
37
48
  - !ruby/object:Gem::Dependency
38
49
  name: riot
39
- requirement: &id003 !ruby/object:Gem::Requirement
50
+ requirement: &id004 !ruby/object:Gem::Requirement
40
51
  none: false
41
52
  requirements:
42
53
  - - ">="
@@ -44,7 +55,7 @@ dependencies:
44
55
  version: 0.12.3
45
56
  type: :development
46
57
  prerelease: false
47
- version_requirements: *id003
58
+ version_requirements: *id004
48
59
  description:
49
60
  email: martinkozak@martinkozak.net
50
61
  executables: []
@@ -92,7 +103,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
92
103
  requirements:
93
104
  - - ">="
94
105
  - !ruby/object:Gem::Version
95
- hash: 2809892554201068801
106
+ hash: 3245774997071353458
96
107
  segments:
97
108
  - 0
98
109
  version: "0"
@@ -105,9 +116,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
116
  requirements: []
106
117
 
107
118
  rubyforge_project:
108
- rubygems_version: 1.8.4
119
+ rubygems_version: 1.8.5
109
120
  signing_key:
110
121
  specification_version: 3
111
- summary: Adds more than 95 useful and frequently rather fundamental methods which are missing in Ruby programming language, to Array, File, Hash, Module, Object, String and Symbol classes. It tries to be similar project to Ruby Facets on principle, but less complex, more practical, non-atomic and organized by better way.
122
+ summary: Adds more than 105 useful and frequently rather fundamental methods which are missing in Ruby programming language, to Array, File, Hash, Module, Object, String and Symbol classes. It tries to be similar project to Ruby Facets on principle, but less complex, more practical, non-atomic and organized by better way.
112
123
  test_files: []
113
124