hash-utils 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.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.3.1"
8
+ s.version = "0.4.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-20}
12
+ s.date = %q{2011-01-27}
13
13
  s.email = %q{martinkozak@martinkozak.net}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE.txt",
@@ -10,7 +10,7 @@ class Array
10
10
  ##
11
11
  # Moves selected values outside the array, so returns them.
12
12
  #
13
- # Works similar to #reject!, but returns removed items instead
13
+ # Works similar to +Hash#reject!+, but returns removed items instead
14
14
  # of remaining items.
15
15
  #
16
16
  # @param [Proc] block selecting block
@@ -36,7 +36,7 @@ class Array
36
36
  # Checks, all values follow condition expressed in block.
37
37
  # Block must return Boolean.
38
38
  #
39
- # If it's empty, returns <tt>true</tt>.
39
+ # If it's empty, returns +true+.
40
40
  #
41
41
  # @param [Proc] block checking block
42
42
  # @return [Boolean] 'true' if yes, 'false' in otherwise
@@ -75,5 +75,34 @@ class Array
75
75
 
76
76
  return false
77
77
  end
78
-
78
+
79
+ ##
80
+ # Converts array to +Hash+.
81
+ #
82
+ # Works as alias for +Hash#[]+ method. If you specify the +:flat+
83
+ # mode, array items will be treaten as arguments to +Hash#[]+
84
+ # method.
85
+ #
86
+ # Should be noted, it cannot be named +#to_hash+, because #to_hash
87
+ # is called by the +Hash#[]+ itself. Reasons why it's absolutely
88
+ # undocumented call are unknown.
89
+ #
90
+ # @example Equivalent calls
91
+ # [["aa", "bb"], ["bb", "aa"]].to_h
92
+ # ["aa", "bb", "bb", "aa"].to_h(:flat)
93
+ #
94
+ # @param [Symbol] mode flat mode switch, can be +:flat+ or +nil+
95
+ # @return Hash new hash
96
+ # @see http://www.ruby-doc.org/core/classes/Hash.html#M000716
97
+ # @since 0.4.0
98
+ #
99
+
100
+ def to_h(mode = nil)
101
+ if mode == :flat
102
+ Hash[*self]
103
+ else
104
+ Hash[self]
105
+ end
106
+ end
107
+
79
108
  end
@@ -9,7 +9,8 @@
9
9
  class Hash
10
10
 
11
11
  ##
12
- # Defines hash by setting the default value or Proc and content.
12
+ # Defines hash by setting the default value or an +Proc+
13
+ # and content.
13
14
  #
14
15
  # @param [Hash] values initial values
15
16
  # @param [Object] default default value
@@ -94,8 +95,7 @@ class Hash
94
95
  end
95
96
 
96
97
  ##
97
- # Returns a copy of <tt>self</tt> with all <tt>nil</tt>
98
- # elements removed.
98
+ # Returns a copy of +self+ with all +nil+ elements removed.
99
99
  #
100
100
  # @return [Hash] new hash
101
101
  # @since 0.1.0
@@ -106,8 +106,8 @@ class Hash
106
106
  end
107
107
 
108
108
  ##
109
- # Removes <tt>nil</tt> elements from the hash. Returns <tt>nil</tt>
110
- # if no changes were made, otherwise returns <tt>self</tt>.
109
+ # Removes +nil+ elements from the hash. Returns +nil+
110
+ # if no changes were made, otherwise returns +self+.
111
111
  #
112
112
  # @return [Hash] new hash
113
113
  # @since 0.1.0
@@ -119,7 +119,7 @@ class Hash
119
119
 
120
120
  ##
121
121
  # Returns a new hash with the results of running block once for
122
- # every pair in <tt>self</tt>.
122
+ # every pair in +self+.
123
123
  #
124
124
  # @param [Proc] block evaluating block
125
125
  # @return [Hash] new hash
@@ -140,7 +140,7 @@ class Hash
140
140
  alias :collect_pairs :map_pairs
141
141
 
142
142
  ##
143
- # Emulates #map_pairs on place. In fact, replaces old hash by
143
+ # Emulates {#map_pairs} on place. In fact, replaces old hash by
144
144
  # new one.
145
145
  #
146
146
  # @param [Proc] block evaluating block
@@ -155,7 +155,7 @@ class Hash
155
155
 
156
156
  ##
157
157
  # Returns a new hash with the results of running block once for
158
- # every key in <tt>self</tt>.
158
+ # every key in +self+.
159
159
  #
160
160
  # @param [Proc] block evaluating block
161
161
  # @return [Hash] new hash
@@ -171,7 +171,7 @@ class Hash
171
171
  alias :collect_keys :map_keys
172
172
 
173
173
  ##
174
- # Emulates #map_keys on place. In fact, replaces old hash by
174
+ # Emulates {#map_keys} on place. In fact, replaces old hash by
175
175
  # new one.
176
176
  #
177
177
  # @param [Proc] block evaluating block
@@ -196,7 +196,7 @@ class Hash
196
196
  end
197
197
 
198
198
  ##
199
- # Emulates #keys_to_sym on place. In fact, replaces old hash by
199
+ # Emulates {#keys_to_sym} on place. In fact, replaces old hash by
200
200
  # new one.
201
201
  #
202
202
  # @since 0.1.0
@@ -208,12 +208,12 @@ class Hash
208
208
 
209
209
  ##
210
210
  # Checks, all elements values follow condition expressed in block.
211
- # Block must return Boolean.
211
+ # Block must return boolean.
212
212
  #
213
- # If it's empty, returns <tt>true</tt>.
213
+ # If it's empty, returns +true+.
214
214
  #
215
215
  # @param [Proc] block checking block
216
- # @return [Boolean] 'true' if yes, 'false' in otherwise
216
+ # @return [Boolean] +true+ if yes, +false+ in otherwise
217
217
  # @since 0.2.0
218
218
  #
219
219
 
@@ -233,12 +233,12 @@ class Hash
233
233
 
234
234
  ##
235
235
  # Checks, all elements follow condition expressed in block.
236
- # Block must return Boolean.
236
+ # Block must return boolean.
237
237
  #
238
- # If it's empty, returns <tt>true</tt>.
238
+ # If it's empty, returns +true+.
239
239
  #
240
240
  # @param [Proc] block checking block
241
- # @return [Boolean] 'true' if yes, 'false' in otherwise
241
+ # @return [Boolean] +true+ if yes, +false+ in otherwise
242
242
  # @since 0.2.0
243
243
  #
244
244
 
@@ -258,10 +258,10 @@ class Hash
258
258
 
259
259
  ##
260
260
  # Checks, at least one element value follows condition expressed in
261
- # block. Block must return Boolean.
261
+ # block. Block must return boolean.
262
262
  #
263
263
  # @param [Proc] block checking block
264
- # @return [Boolean] 'true' if yes, 'false' in otherwise
264
+ # @return [Boolean] +true+ if yes, +false+ in otherwise
265
265
  # @since 0.2.0
266
266
  #
267
267
 
@@ -277,10 +277,10 @@ class Hash
277
277
 
278
278
  ##
279
279
  # Checks, at least one element follows condition expressed in
280
- # block. Block must return Boolean.
280
+ # block. Block must return boolean.
281
281
  #
282
282
  # @param [Proc] block checking block
283
- # @return [Boolean] 'true' if yes, 'false' in otherwise
283
+ # @return [Boolean] +true+ if yes, +false+ in otherwise
284
284
  # @since 0.2.0
285
285
  #
286
286
 
@@ -293,4 +293,15 @@ class Hash
293
293
 
294
294
  return false
295
295
  end
296
+
297
+ ##
298
+ # Compatibility method with {Array#to_h}. Returns itself.
299
+ #
300
+ # @return [Hash] itself
301
+ # @since 0.4.0
302
+ #
303
+
304
+ def to_h(mode = nil)
305
+ self
306
+ end
296
307
  end
@@ -7,10 +7,10 @@
7
7
 
8
8
  class Numeric
9
9
  ##
10
- # Indicates numeric is in some object which supports #include?.
10
+ # Indicates numeric is in some object which supports +#include?+.
11
11
  #
12
12
  # @param [Object] range container for analyzing
13
- # @param [Boolean] 'true' if yes, 'false' in otherwise
13
+ # @return [Boolean] +true+ if yes, +false+ in otherwise
14
14
  # @since 0.3.0
15
15
  #
16
16
 
@@ -1,6 +1,9 @@
1
1
  # encoding: utf-8
2
2
  # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
3
 
4
+ require "hash-utils/array"
5
+ require "hash-utils/hash"
6
+
4
7
  ##
5
8
  # String extension.
6
9
  #
@@ -13,10 +16,10 @@ class String
13
16
  NUMERIC = /^\s*-?\d+(?:\.\d+)?\s*$/
14
17
 
15
18
  ##
16
- # Indicates string is in some object which supports #include?.
19
+ # Indicates string is in some object which supports +#include?+.
17
20
  #
18
21
  # @param [Object] range container for analyzing
19
- # @param [Boolean] 'true' if yes, 'false' in otherwise
22
+ # @return [Boolean] +true+ if yes, +false+ in otherwise
20
23
  # @since 0.3.0
21
24
  #
22
25
 
@@ -27,7 +30,7 @@ class String
27
30
  ##
28
31
  # Indicates, string is numeric, so consists of numbers only.
29
32
  #
30
- # @return [Boolean] 'true' if yes, 'false' in otherwise
33
+ # @return [Boolean] +true+ if yes, +false+ in otherwise
31
34
  # @since 0.3.0
32
35
  #
33
36
 
@@ -38,4 +41,80 @@ class String
38
41
  false
39
42
  end
40
43
  end
44
+
45
+ ##
46
+ # Replaces set of substrings by another strings.
47
+ #
48
+ # It's equivalent of PHP +strtr()+ function. Supports all objects
49
+ # convertable to String, but in that case you must give him block
50
+ # which specifies how to map keys found in the string back to
51
+ # keys in definition Hash or Array.
52
+ #
53
+ # If you specify the +:flat+ mode, definition array items will be
54
+ # treaten as arguments to +Hash#[]+ method while internal conversion
55
+ # +Array+ to +Hash+, so then can then use plain array as base for
56
+ # definitions. See {Array#to_h}.
57
+ #
58
+ # @example Equivalent calls
59
+ # "aa bb".tr("aa" => "bb", "bb" => "aa")
60
+ # "aa bb".tr([["aa", "bb"], ["bb", "aa"]])
61
+ # "aa bb".tr(["aa", "bb", "bb", "aa"], :flat)
62
+ # @example Use with symbols
63
+ # "aa bb".tr(:aa => "bb", :bb => "aa") { |s| s.to_sym }
64
+ #
65
+ # @param [Array, Hash] replacements replacements definition
66
+ # @param [Symbol] mode flat mode switch, can be +:flat+ or +nil+
67
+ # @param [Proc] block with keys mapping worker (see description)
68
+ # @return [String] string with applied replacements
69
+ # @see http://www.php.net/strtr
70
+ # @since 0.4.0
71
+ #
72
+
73
+ def tr(defs, mode = nil, &block)
74
+ if block.nil?
75
+ block = Proc::new { |s| s }
76
+ end
77
+
78
+ defs, matcher = __prepare_tr(defs, mode)
79
+ self.gsub(matcher) { |s| defs[block.call(s)] }
80
+ end
81
+
82
+ ##
83
+ # Replaces set of substrings by another strings -- in place.
84
+ # See {#tr} for details.
85
+ #
86
+ # @param [Array, Hash] replacements replacements definition
87
+ # @param [Symbol] mode flat mode switch, can be +:flat+ or +nil+
88
+ # @param [Proc] block with keys mapping worker (see description)
89
+ # @return [String] string with applied replacements
90
+ # @see #tr
91
+ # @since 0.4.0
92
+ #
93
+
94
+ def tr!(defs, mode = nil, &block)
95
+ if block.nil?
96
+ block = Proc::new { |s| s }
97
+ end
98
+
99
+ defs, matcher = __prepare_tr(defs, mode)
100
+ self.gsub!(matcher) { |s| defs[block.call(s)] }
101
+ end
102
+
103
+
104
+ private
105
+
106
+ ##
107
+ # Prepares matcher for #tr.
108
+ #
109
+
110
+ def __prepare_tr(defs, mode = nil)
111
+ defs = defs.to_h(mode)
112
+ keys = defs.keys
113
+ keys.map! { |i| i.to_s }
114
+
115
+ matcher = Regexp::new("(" << keys.join("|") << ")")
116
+ return [defs, matcher]
117
+ end
118
+
41
119
  end
120
+
@@ -1,17 +1,16 @@
1
1
  # encoding: utf-8
2
2
  # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
3
 
4
-
5
4
  ##
6
5
  # Symbol extension.
7
6
  #
8
7
 
9
8
  class Symbol
10
9
  ##
11
- # Indicates symbol is in some object which supports #include?.
10
+ # Indicates symbol is in some object which supports +#include?+.
12
11
  #
13
12
  # @param [Object] range container for analyzing
14
- # @param [Boolean] 'true' if yes, 'false' in otherwise
13
+ # @return [Boolean] +true+ if yes, +false+ in otherwise
15
14
  # @since 0.3.0
16
15
  #
17
16
 
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
8
- - 1
9
- version: 0.3.1
7
+ - 4
8
+ - 0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Martin Koz\xC3\xA1k"
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-20 00:00:00 +01:00
17
+ date: 2011-01-27 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -85,7 +85,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
85
  requirements:
86
86
  - - ">="
87
87
  - !ruby/object:Gem::Version
88
- hash: -3792094260852597544
88
+ hash: -2173078949631025708
89
89
  segments:
90
90
  - 0
91
91
  version: "0"