rhash 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rhash.rb +162 -15
- data/test/test.rb +15 -8
- metadata +18 -6
data/lib/rhash.rb
CHANGED
@@ -14,18 +14,51 @@ class Info
|
|
14
14
|
end
|
15
15
|
|
16
16
|
class RHash
|
17
|
+
#
|
18
|
+
# Returns a new, empty RHash.
|
19
|
+
# If a block is specified, it will be called with the RHash object and the key,
|
20
|
+
# and should return the default value. It is the block‘s responsibility to
|
21
|
+
# store the value in the RHash if required.
|
22
|
+
#
|
17
23
|
def initialize(&block)
|
18
24
|
@hash = []
|
25
|
+
@default = nil
|
19
26
|
@default = block if block_given?
|
20
27
|
end
|
21
28
|
|
29
|
+
#
|
30
|
+
# Returns value corresponding to key.
|
31
|
+
# If the key wasn't found, an IndexError exception will be raised.
|
32
|
+
#
|
33
|
+
# my_hash = RHash.new
|
34
|
+
#
|
35
|
+
# my_hash["cow"] = "mooo..."
|
36
|
+
#
|
37
|
+
# my_hash["cow"] #=> "mooo..."
|
38
|
+
#
|
39
|
+
# my_hash["chick"] #=> nil
|
40
|
+
#
|
22
41
|
def [] key
|
23
42
|
@hash.each do |info|
|
24
43
|
return info.value if key == info.key
|
25
44
|
end
|
26
|
-
|
45
|
+
(@default.is_a? Proc)? @default.call : @default
|
27
46
|
end
|
28
47
|
|
48
|
+
#
|
49
|
+
# Associates the value given by value with the key given by key.
|
50
|
+
# If already exists a key then key value will be updated.
|
51
|
+
#
|
52
|
+
# my_hash = RHash.new
|
53
|
+
#
|
54
|
+
# my_hash["cow"] = "mooo..."
|
55
|
+
#
|
56
|
+
# my_hash["cow"] #=> "mooo..."
|
57
|
+
#
|
58
|
+
# my_hash["cow"] = "cocó..."
|
59
|
+
#
|
60
|
+
# my_hash["cow"] #=> "cocó..."
|
61
|
+
#
|
29
62
|
def []= key, value
|
30
63
|
info = Info.new
|
31
64
|
info.key = key
|
@@ -38,6 +71,9 @@ class RHash
|
|
38
71
|
end
|
39
72
|
end
|
40
73
|
|
74
|
+
#
|
75
|
+
# Return the contents of this hash as a string.
|
76
|
+
#
|
41
77
|
def inspect
|
42
78
|
msg = '{'
|
43
79
|
@hash.each_with_index do |info, index|
|
@@ -48,71 +84,113 @@ class RHash
|
|
48
84
|
msg
|
49
85
|
end
|
50
86
|
|
51
|
-
|
52
|
-
|
87
|
+
#
|
88
|
+
# Returns a RHash orderned by key name or block.
|
89
|
+
# If block is given using Array#sort else sort by name.
|
90
|
+
#
|
91
|
+
def sort(&block)
|
92
|
+
sort_rhash = RHash.new
|
93
|
+
int_sort(&block).each do |info|
|
94
|
+
sort_rhash[info[0]] = info[1]
|
95
|
+
end
|
96
|
+
sort_rhash
|
53
97
|
end
|
54
98
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
99
|
+
#
|
100
|
+
# Sorts self.
|
101
|
+
# If block is given using Array#sort else sort by name.
|
102
|
+
#
|
103
|
+
def sort!(&block)
|
104
|
+
sort_hash = int_sort(&block)
|
105
|
+
clear
|
106
|
+
sort_hash.each do |info|
|
107
|
+
self[info[0]] = info[1]
|
108
|
+
end
|
59
109
|
end
|
60
110
|
|
111
|
+
#
|
112
|
+
# Orderned RHash and calls block once for each key in RHash,
|
113
|
+
# passing the key and value to the block as a two-element array
|
114
|
+
#
|
61
115
|
def sort_each(&block)
|
62
116
|
new_hash = int_sort
|
63
117
|
new_hash.each do |info|
|
64
|
-
yield info if block_given?
|
118
|
+
yield [info.key, info.value] if block_given?
|
65
119
|
end
|
66
120
|
end
|
67
121
|
|
122
|
+
#
|
123
|
+
# See Hash::each
|
124
|
+
#
|
68
125
|
def each(&block)
|
69
126
|
@hash.each do |info|
|
70
|
-
yield info if block_given?
|
127
|
+
yield [info.key, info.value] if block_given?
|
71
128
|
end
|
72
129
|
end
|
73
130
|
|
131
|
+
#
|
132
|
+
# See Hash::each_pair
|
133
|
+
#
|
74
134
|
def each_pair(&block)
|
75
135
|
@hash.each do |info|
|
76
136
|
yield info.key, info.value if block_given?
|
77
137
|
end
|
78
138
|
end
|
79
139
|
|
140
|
+
#
|
141
|
+
# See Hash::each_value
|
142
|
+
#
|
80
143
|
def each_value(&block)
|
81
144
|
array(:value).each do |value|
|
82
145
|
yield value if block_given?
|
83
146
|
end
|
84
147
|
end
|
85
148
|
|
149
|
+
#
|
150
|
+
# See Hash::each_key
|
151
|
+
#
|
86
152
|
def each_key(&block)
|
87
153
|
array(:key).each do |value|
|
88
154
|
yield value if block_given?
|
89
155
|
end
|
90
156
|
end
|
91
157
|
|
158
|
+
#
|
159
|
+
# See Hash::select
|
160
|
+
#
|
92
161
|
def select(&block)
|
93
162
|
selected_entries = []
|
94
163
|
@hash.each do |info|
|
95
164
|
if block_given?
|
96
165
|
if yield info
|
97
|
-
selected_entries << info
|
166
|
+
selected_entries << [info.key, info.value]
|
98
167
|
end
|
99
168
|
end
|
100
169
|
end
|
101
170
|
selected_entries
|
102
171
|
end
|
103
172
|
|
173
|
+
#
|
174
|
+
# See Hash::keys
|
175
|
+
#
|
104
176
|
def keys
|
105
177
|
new_array = []
|
106
178
|
@hash.each{|info| new_array << info.key}
|
107
179
|
new_array
|
108
180
|
end
|
109
181
|
|
182
|
+
#
|
183
|
+
# See Hash::values
|
184
|
+
#
|
110
185
|
def values
|
111
186
|
new_array = []
|
112
187
|
@hash.each{|info| new_array << info.value}
|
113
188
|
new_array
|
114
189
|
end
|
115
190
|
|
191
|
+
#
|
192
|
+
# See Hash::key?
|
193
|
+
#
|
116
194
|
def has_key?(key)
|
117
195
|
@hash.each{|info| return true if info.key == key}
|
118
196
|
false
|
@@ -120,16 +198,25 @@ class RHash
|
|
120
198
|
alias :key? :has_key?
|
121
199
|
alias :member? :has_key?
|
122
200
|
|
201
|
+
#
|
202
|
+
# See Hash::value?
|
203
|
+
#
|
123
204
|
def has_value?(value)
|
124
205
|
@hash.each{|info| return true if info.value == value}
|
125
206
|
false
|
126
207
|
end
|
127
208
|
alias :value? :has_value?
|
128
209
|
|
210
|
+
#
|
211
|
+
# See Hash::delete_if
|
212
|
+
#
|
129
213
|
def delete_if(&block)
|
130
214
|
@hash.delete_if{|info| yield info.key, info.value if block_given?}
|
131
215
|
end
|
132
216
|
|
217
|
+
#
|
218
|
+
# See Hash::delete
|
219
|
+
#
|
133
220
|
def delete(key, &block)
|
134
221
|
deleted = nil
|
135
222
|
@hash.delete_if{|info| info.key == key; deleted = info if info.key == key}
|
@@ -137,11 +224,18 @@ class RHash
|
|
137
224
|
return "Deleted: #{deleted.key.inspect} => #{deleted.value.inspect}" if deleted
|
138
225
|
end
|
139
226
|
|
227
|
+
#
|
228
|
+
# See Hash::size
|
229
|
+
#
|
140
230
|
def size
|
141
231
|
@hash.size
|
142
232
|
end
|
233
|
+
|
143
234
|
alias :length :size
|
144
235
|
|
236
|
+
#
|
237
|
+
# See Hash::invert
|
238
|
+
#
|
145
239
|
def invert
|
146
240
|
new_hash = RHash.new
|
147
241
|
@hash.each do |i|
|
@@ -149,11 +243,17 @@ class RHash
|
|
149
243
|
end
|
150
244
|
new_hash
|
151
245
|
end
|
152
|
-
|
246
|
+
|
247
|
+
#
|
248
|
+
# See RHash::inspect
|
249
|
+
#
|
153
250
|
def to_s
|
154
251
|
inspect
|
155
252
|
end
|
156
253
|
|
254
|
+
#
|
255
|
+
# See Hash::merge
|
256
|
+
#
|
157
257
|
def merge(rhash)
|
158
258
|
new_hash = RHash.new
|
159
259
|
raise "Não é uma Hash ou RHash" unless rhash.is_a? RHash or rhash.is_a? Hash
|
@@ -176,6 +276,9 @@ class RHash
|
|
176
276
|
new_hash
|
177
277
|
end
|
178
278
|
|
279
|
+
#
|
280
|
+
# See Hash::merge!
|
281
|
+
#
|
179
282
|
def merge!(rhash)
|
180
283
|
raise "Não é uma Hash ou RHash" unless rhash.is_a? RHash or rhash.is_a? Hash
|
181
284
|
if rhash.is_a? RHash
|
@@ -189,14 +292,24 @@ class RHash
|
|
189
292
|
end
|
190
293
|
end
|
191
294
|
|
295
|
+
#
|
296
|
+
# Returns an array with first key and value found in RHash.
|
297
|
+
#
|
192
298
|
def first
|
193
|
-
@hash.first
|
299
|
+
first = @hash.first
|
300
|
+
[first.key, first.value]
|
194
301
|
end
|
195
302
|
|
303
|
+
#
|
304
|
+
# Returns an array with last key and value found in RHash.
|
305
|
+
#
|
196
306
|
def last
|
197
307
|
@hash.last
|
198
308
|
end
|
199
309
|
|
310
|
+
#
|
311
|
+
# See Hash::fetch
|
312
|
+
#
|
200
313
|
def fetch(key, msg='', &block)
|
201
314
|
value = select{|i| i.key == key}[0]
|
202
315
|
|
@@ -207,14 +320,23 @@ class RHash
|
|
207
320
|
end
|
208
321
|
end
|
209
322
|
|
323
|
+
#
|
324
|
+
# See Hash::clear
|
325
|
+
#
|
210
326
|
def clear
|
211
327
|
@hash = []
|
212
328
|
end
|
213
329
|
|
330
|
+
#
|
331
|
+
# See Hash::default=
|
332
|
+
#
|
214
333
|
def default=(value)
|
215
334
|
@default = value
|
216
335
|
end
|
217
336
|
|
337
|
+
#
|
338
|
+
# See Hash::default
|
339
|
+
#
|
218
340
|
def default(key)
|
219
341
|
if @default.is_a? Proc
|
220
342
|
info = Info.new
|
@@ -229,19 +351,31 @@ class RHash
|
|
229
351
|
end
|
230
352
|
end
|
231
353
|
|
354
|
+
#
|
355
|
+
# See Hash::shift
|
356
|
+
#
|
232
357
|
def shift
|
233
358
|
shifted = @hash.shift
|
234
359
|
[shifted.key, shifted.value]
|
235
360
|
end
|
236
361
|
|
362
|
+
#
|
363
|
+
# See Hash::empty?
|
364
|
+
#
|
237
365
|
def empty?
|
238
366
|
@hash.empty?
|
239
367
|
end
|
240
368
|
|
369
|
+
#
|
370
|
+
# See Hash::store
|
371
|
+
#
|
241
372
|
def store(key, value)
|
242
373
|
self[key] = value
|
243
374
|
end
|
244
375
|
|
376
|
+
#
|
377
|
+
# See Hash::to_a
|
378
|
+
#
|
245
379
|
def to_a
|
246
380
|
array = []
|
247
381
|
@hash.each do |info|
|
@@ -250,6 +384,9 @@ class RHash
|
|
250
384
|
array
|
251
385
|
end
|
252
386
|
|
387
|
+
#
|
388
|
+
# See Hash::values_at
|
389
|
+
#
|
253
390
|
def values_at(*keys)
|
254
391
|
selected = []
|
255
392
|
@hash.each do |info|
|
@@ -258,7 +395,9 @@ class RHash
|
|
258
395
|
selected
|
259
396
|
end
|
260
397
|
|
261
|
-
#
|
398
|
+
#
|
399
|
+
# Converts to a Hash.
|
400
|
+
#
|
262
401
|
def to_hash
|
263
402
|
pure_hash = {}
|
264
403
|
@hash.each do |info|
|
@@ -269,9 +408,17 @@ class RHash
|
|
269
408
|
|
270
409
|
private
|
271
410
|
|
272
|
-
def int_sort
|
411
|
+
def int_sort(&block)
|
273
412
|
new_hash = []
|
274
|
-
|
413
|
+
if !block_given?
|
414
|
+
new_hash = @hash.sort_by{|info| info.key}
|
415
|
+
else
|
416
|
+
aux = []
|
417
|
+
@hash.each do |info|
|
418
|
+
aux << [info.key, info.value]
|
419
|
+
end
|
420
|
+
new_hash = aux.sort(&block)
|
421
|
+
end
|
275
422
|
new_hash
|
276
423
|
end
|
277
424
|
|
data/test/test.rb
CHANGED
@@ -1,19 +1,26 @@
|
|
1
1
|
require 'lib/rhash'
|
2
2
|
|
3
3
|
n = RHash.new
|
4
|
-
n[
|
5
|
-
n[
|
6
|
-
n[
|
7
|
-
n[
|
4
|
+
n['b'] = 400
|
5
|
+
n['g'] = 5
|
6
|
+
n["a"] = 8
|
7
|
+
n["x"] = 105
|
8
|
+
n["xx"] = 1054
|
8
9
|
|
9
|
-
puts n.inspect
|
10
|
-
n.sort!
|
11
|
-
puts n.inspect
|
10
|
+
#puts n.inspect
|
11
|
+
#n.sort!{|i,j| i[0] <=> j[0]}
|
12
|
+
#puts n.inspect
|
13
|
+
#x = n.sort{|i,j| i[1] <=> j[1]}
|
14
|
+
#puts x.class
|
15
|
+
#puts x.inspect
|
16
|
+
#puts n.inspect
|
17
|
+
#puts n[:s]
|
12
18
|
#n.sort_each{|i| puts i}
|
19
|
+
#n.each{|i| puts i}
|
13
20
|
#n.each_pair{|i, j| puts i; puts j+1}
|
14
21
|
#n.each_value{|i| puts i+10}
|
15
22
|
#n.each_key{|i| puts i.inspect}
|
16
|
-
|
23
|
+
puts n.select{|i| i.value > 100}
|
17
24
|
#puts n.keys.inspect
|
18
25
|
#puts n.values.inspect
|
19
26
|
#puts n.has_key?("a")
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rhash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 25
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Rodrigo Mello Nardi
|
@@ -9,7 +15,7 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date: 2010-07-
|
18
|
+
date: 2010-07-14 00:00:00 -03:00
|
13
19
|
default_executable:
|
14
20
|
dependencies: []
|
15
21
|
|
@@ -25,7 +31,7 @@ files:
|
|
25
31
|
- lib/rhash.rb
|
26
32
|
- test/test.rb
|
27
33
|
has_rdoc: true
|
28
|
-
homepage:
|
34
|
+
homepage: http://rubyhideoutprogramming.blogspot.com/
|
29
35
|
licenses: []
|
30
36
|
|
31
37
|
post_install_message:
|
@@ -34,21 +40,27 @@ rdoc_options: []
|
|
34
40
|
require_paths:
|
35
41
|
- lib
|
36
42
|
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
37
44
|
requirements:
|
38
45
|
- - ">="
|
39
46
|
- !ruby/object:Gem::Version
|
47
|
+
hash: 3
|
48
|
+
segments:
|
49
|
+
- 0
|
40
50
|
version: "0"
|
41
|
-
version:
|
42
51
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
43
53
|
requirements:
|
44
54
|
- - ">="
|
45
55
|
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
46
59
|
version: "0"
|
47
|
-
version:
|
48
60
|
requirements: []
|
49
61
|
|
50
62
|
rubyforge_project:
|
51
|
-
rubygems_version: 1.3.
|
63
|
+
rubygems_version: 1.3.7
|
52
64
|
signing_key:
|
53
65
|
specification_version: 3
|
54
66
|
summary: an ordenable hash
|