hash-utils 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,56 +1,12 @@
1
1
  Hash Utils
2
2
  ==========
3
3
 
4
- **Hash Utils** adds some utility methods well known from [Array][1]
5
- class to [Hash][2] class. Extends them with following methods:
4
+ **Hash Utils** adds a lot of useful fundamental utility methods which
5
+ are missing in Ruby, both to [Array][1] and [Hash][2] classes and
6
+ introduces some useful methods and syntactic sugar to [Numeric][7],
7
+ [Symbol][6] and [String][8] classes too.
6
8
 
7
- * [`#compact`][3] rejects elements with `nil` values,
8
- * [`#compact!`][4]
9
- * `#map_pairs` – works as [Array#map][5], but for *whole pair*,
10
- * `#map_pairs!`
11
- * `#map_keys` – works as [Array#map][5], but for *keys only*,
12
- * `#map_keys!`
13
- * `#keys_to_sym` – converts all keys to [Symbols][6],
14
- * `#keys_to_sym!`
15
- * …
16
-
17
- All methods with `!` emulates work *in place*, but in fact they
18
- replace old hash with new one. An example of use:
19
-
20
- foo = {"a" => 1, "b" => 2}
21
- foo.map_keys! { |k| k.to_sym }
22
-
23
- # result will be
24
- # {:a => 1, :b => 2}
25
-
26
- This is the same as `#keys_to_sym!` for example. And also introduces
27
- some methods known from Python to both [Array][1] and [Hash][2] class:
28
-
29
- * `some?` – returns `true` if *some* element follows condition in block,
30
- * `some_pairs?`
31
- * `all?` – returns `true` if *all* element follows condition in block,
32
- * `all_pairs?`
33
-
34
- For example:
35
-
36
- foo = [:alfa, :beta, 5, :gama]
37
- foo.some? { |i| i.kind_of? Numeric } # returns true
38
- foo.all? { |i| i.kind_of? Symbol } # returns false
39
-
40
- And finally introduces one method to [Numeric][7], [Symbol][6] and
41
- [String][8]:
42
-
43
- * `in?` – returns `true` if value is in some Object which support
44
- `include?` method
45
-
46
- For example:
47
-
48
- foo = [1, 2, 3, 5, 8]
49
- 5.in? foo # returns true
50
- 5.in? 3..6 # returns true
51
-
52
- As you can see, it's syntactic sugar, of sure, but useful in some cases.
53
- Except these, it adds many other useful metods. See documentation.
9
+ For full reference and methods lists, see **[documentation][3]**.
54
10
 
55
11
  ### Modularization
56
12
 
@@ -78,9 +34,7 @@ further details.
78
34
 
79
35
  [1]: http://www.ruby-doc.org/core/classes/Array.html
80
36
  [2]: http://www.ruby-doc.org/core/classes/Hash.html
81
- [3]: http://www.ruby-doc.org/core/classes/Array.html#M000278
82
- [4]: http://www.ruby-doc.org/core/classes/Array.html#M000279
83
- [5]: http://www.ruby-doc.org/core/classes/Array.html#M000249
37
+ [3]: http://rubydoc.info/gems/hash-utils
84
38
  [6]: http://www.ruby-doc.org/core/classes/Symbol.html
85
39
  [7]: http://www.ruby-doc.org/core/classes/Numeric.html
86
40
  [8]: http://www.ruby-doc.org/core/classes/String.html
data/Rakefile CHANGED
@@ -16,7 +16,7 @@ Jeweler::Tasks.new do |gem|
16
16
  gem.name = "hash-utils"
17
17
  gem.homepage = "http://github.com/martinkozak/hash-utils"
18
18
  gem.license = "MIT"
19
- gem.summary = 'Adds some useful well known methods similar to appropriate Array methods to Hash and some useful methods known from Python to both of them. Also defines some utility method for other data types.'
19
+ gem.summary = 'Adds a lot of useful fundamental utility methods which are missing in Ruby, both to Array and Hash classes and introduces some useful methods and syntactic sugar to Numeric, Symbol and String classes too.'
20
20
  gem.email = "martinkozak@martinkozak.net"
21
21
  gem.authors = ["Martin Kozák"]
22
22
  # Include your dependencies below. Runtime dependencies are required when using your gem,
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.1
1
+ 0.5.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.4.1"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Martin Kozák"]
12
- s.date = %q{2011-01-27}
12
+ s.date = %q{2011-02-08}
13
13
  s.email = %q{martinkozak@martinkozak.net}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE.txt",
@@ -34,11 +34,10 @@ Gem::Specification.new do |s|
34
34
  s.homepage = %q{http://github.com/martinkozak/hash-utils}
35
35
  s.licenses = ["MIT"]
36
36
  s.require_paths = ["lib"]
37
- s.rubygems_version = %q{1.3.7}
38
- s.summary = %q{Adds some useful well known methods similar to appropriate Array methods to Hash and some useful methods known from Python to both of them. Also defines some utility method for other data types.}
37
+ s.rubygems_version = %q{1.5.0}
38
+ s.summary = %q{Adds a lot of useful fundamental utility methods which are missing in Ruby, both to Array and Hash classes and introduces some useful methods and syntactic sugar to Numeric, Symbol and String classes too.}
39
39
 
40
40
  if s.respond_to? :specification_version then
41
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
42
41
  s.specification_version = 3
43
42
 
44
43
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
@@ -39,7 +39,7 @@ class Array
39
39
  # If it's empty, returns +true+.
40
40
  #
41
41
  # @param [Proc] block checking block
42
- # @return [Boolean] 'true' if yes, 'false' in otherwise
42
+ # @return [Boolean] +true+ if yes, +false+ in otherwise
43
43
  # @since 0.2.0
44
44
  #
45
45
 
@@ -62,7 +62,7 @@ class Array
62
62
  # block. Block must return Boolean.
63
63
  #
64
64
  # @param [Proc] block checking block
65
- # @return [Boolean] 'true' if yes, 'false' in otherwise
65
+ # @return [Boolean] +true+ if yes, +false+ in otherwise
66
66
  # @since 0.2.0
67
67
  #
68
68
 
@@ -60,6 +60,7 @@ class Hash
60
60
  # Recreates the hash in place, so creates empty one, assigns
61
61
  # the same default values and replaces the old one.
62
62
  #
63
+ # @return [Hash] new hash
63
64
  # @since 0.3.0
64
65
  #
65
66
 
@@ -144,6 +145,7 @@ class Hash
144
145
  # new one.
145
146
  #
146
147
  # @param [Proc] block evaluating block
148
+ # @return [Hash] new hash
147
149
  # @since 0.1.0
148
150
  #
149
151
 
@@ -175,6 +177,7 @@ class Hash
175
177
  # new one.
176
178
  #
177
179
  # @param [Proc] block evaluating block
180
+ # @return [Hash] new hash
178
181
  # @since 0.1.0
179
182
  #
180
183
 
@@ -199,6 +202,7 @@ class Hash
199
202
  # Emulates {#keys_to_sym} on place. In fact, replaces old hash by
200
203
  # new one.
201
204
  #
205
+ # @return [Hash] new hash
202
206
  # @since 0.1.0
203
207
  #
204
208
 
@@ -304,4 +308,109 @@ class Hash
304
308
  def to_h(mode = nil)
305
309
  self
306
310
  end
311
+
312
+ ##
313
+ # Simulates sorting in place. In fact, replaces old one by new one.
314
+ #
315
+ # @param [Proc] block comparing block (see similar for #sort method)
316
+ # @return [Hash] new sorted hash
317
+ # @since 0.5.0
318
+ #
319
+
320
+ def sort!(&block)
321
+ self.replace(Hash[self.sort(&block)])
322
+ end
323
+
324
+ ##
325
+ # Sorts hash according to keys. It's equivalent of PHP ksort().
326
+ #
327
+ # Be warn, this method have sense in Ruby 1.9 only. Ruby 1.8 doesn't
328
+ # maintain pair order in Hashes.
329
+ #
330
+ # @param [Proc] block comparing block (see similar for #sort method)
331
+ # @return [Hash] new sorted hash
332
+ # @see http://www.igvita.com/2009/02/04/ruby-19-internals-ordered-hash/
333
+ # @see http://www.php.net/ksort
334
+ # @since 0.5.0
335
+ #
336
+
337
+ def ksort(&block)
338
+ if block.nil?
339
+ block = Proc::new { |a, b| a <=> b }
340
+ end
341
+ Hash[self.sort { |a, b| block.call(a[0], b[0]) }]
342
+ end
343
+
344
+ ##
345
+ # Sorts hash according to keys, replaces the original one.
346
+ #
347
+ # @param [Proc] block comparing block (see similar for #sort method)
348
+ # @return [Hash] new sorted hash
349
+ # @see #ksort
350
+ # @since 0.5.0
351
+ #
352
+
353
+ def ksort!(&block)
354
+ self.replace(self.ksort(&block))
355
+ end
356
+
357
+ ##
358
+ # Sorts hash according to values. Keeps key associations.
359
+ # It's equivalent of PHP asort().
360
+ #
361
+ # Be warn, this method have sense in Ruby 1.9 only. Ruby 1.8 doesn't
362
+ # maintain pair order in Hashes.
363
+ #
364
+ # @param [Proc] block comparing block (see similar for #sort method)
365
+ # @return [Hash] new sorted hash
366
+ # @see http://www.igvita.com/2009/02/04/ruby-19-internals-ordered-hash/
367
+ # @see http://www.php.net/asort
368
+ # @since 0.5.0
369
+ #
370
+
371
+ def asort(&block)
372
+ if block.nil?
373
+ block = Proc::new { |a, b| a <=> b }
374
+ end
375
+ Hash[self.sort { |a, b| block.call(a[1], b[1]) }]
376
+ end
377
+
378
+ ##
379
+ # Sorts hash according to values, replaces the original one.
380
+ #
381
+ # @param [Proc] block comparing block (see similar for #sort method)
382
+ # @return [Hash] reversed hash
383
+ # @see #asort
384
+ # @since 0.5.0
385
+ #
386
+
387
+ def asort!(&block)
388
+ self.replace(self.asort(&block))
389
+ end
390
+
391
+ ##
392
+ # Reverses order of the hash pairs.
393
+ #
394
+ # Be warn, this method have sense in Ruby 1.9 only. Ruby 1.8 doesn't
395
+ # maintain pair order in Hashes.
396
+ #
397
+ # @return [Hash] reversed hash
398
+ # @since 0.5.0
399
+ #
400
+
401
+ def reverse
402
+ Hash[self.to_a.reverse]
403
+ end
404
+
405
+ ##
406
+ # Reverses order of the hash pairs, replaces the original one.
407
+ #
408
+ # @return [Hash] reversed hash
409
+ # @since 0.5.0
410
+ #
411
+
412
+ def reverse!
413
+ self.replace(self.reverse)
414
+ end
415
+
307
416
  end
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash-utils
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 4
8
- - 1
9
- version: 0.4.1
4
+ prerelease:
5
+ version: 0.5.0
10
6
  platform: ruby
11
7
  authors:
12
8
  - "Martin Koz\xC3\xA1k"
@@ -14,7 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2011-01-27 00:00:00 +01:00
13
+ date: 2011-02-08 00:00:00 +01:00
18
14
  default_executable:
19
15
  dependencies:
20
16
  - !ruby/object:Gem::Dependency
@@ -24,10 +20,6 @@ dependencies:
24
20
  requirements:
25
21
  - - ~>
26
22
  - !ruby/object:Gem::Version
27
- segments:
28
- - 1
29
- - 0
30
- - 0
31
23
  version: 1.0.0
32
24
  type: :development
33
25
  prerelease: false
@@ -39,10 +31,6 @@ dependencies:
39
31
  requirements:
40
32
  - - ~>
41
33
  - !ruby/object:Gem::Version
42
- segments:
43
- - 1
44
- - 5
45
- - 2
46
34
  version: 1.5.2
47
35
  type: :development
48
36
  prerelease: false
@@ -85,7 +73,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
73
  requirements:
86
74
  - - ">="
87
75
  - !ruby/object:Gem::Version
88
- hash: 1545650960635711712
76
+ hash: 1593935056152751223
89
77
  segments:
90
78
  - 0
91
79
  version: "0"
@@ -94,15 +82,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
82
  requirements:
95
83
  - - ">="
96
84
  - !ruby/object:Gem::Version
97
- segments:
98
- - 0
99
85
  version: "0"
100
86
  requirements: []
101
87
 
102
88
  rubyforge_project:
103
- rubygems_version: 1.3.7
89
+ rubygems_version: 1.5.0
104
90
  signing_key:
105
91
  specification_version: 3
106
- summary: Adds some useful well known methods similar to appropriate Array methods to Hash and some useful methods known from Python to both of them. Also defines some utility method for other data types.
92
+ summary: Adds a lot of useful fundamental utility methods which are missing in Ruby, both to Array and Hash classes and introduces some useful methods and syntactic sugar to Numeric, Symbol and String classes too.
107
93
  test_files: []
108
94