rhash 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rhash.rb +160 -32
- data/test/test.rb +23 -71
- metadata +4 -4
data/lib/rhash.rb
CHANGED
@@ -12,7 +12,7 @@ class Info
|
|
12
12
|
"#{key.inspect} => #{value.inspect}"
|
13
13
|
end
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
class RHash
|
17
17
|
#
|
18
18
|
# Returns a new, empty RHash.
|
@@ -71,23 +71,24 @@ class RHash
|
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
|
-
#
|
75
|
-
# Return the contents of this hash as a string.
|
76
|
-
#
|
77
|
-
def inspect
|
78
|
-
msg = '{'
|
79
|
-
@hash.each_with_index do |info, index|
|
80
|
-
msg += info.to_s
|
81
|
-
msg += ", " if @hash.size > 1 and index + 1 < @hash.size
|
82
|
-
end
|
83
|
-
msg += '}'
|
84
|
-
msg
|
85
|
-
end
|
86
|
-
|
87
74
|
#
|
88
75
|
# Returns a RHash orderned by key name or block.
|
89
76
|
# If block is given using Array#sort else sort by name.
|
90
77
|
#
|
78
|
+
# my_hash = RHash.new
|
79
|
+
#
|
80
|
+
# my_hash["cow"] = "mooo..."
|
81
|
+
#
|
82
|
+
# my_hash["chick"] = "cocó..."
|
83
|
+
#
|
84
|
+
# my_hash["bee"] = "ziiii..."
|
85
|
+
#
|
86
|
+
# my_hash.sort #=> Returns RHash object.
|
87
|
+
#
|
88
|
+
# sort_hash = my_hash.sort
|
89
|
+
#
|
90
|
+
# sort_hash.inspect #=> {"bee" => "ziiii...", "chick" => "cocó...", "cow" => "mooo..."}
|
91
|
+
#
|
91
92
|
def sort(&block)
|
92
93
|
sort_rhash = RHash.new
|
93
94
|
int_sort(&block).each do |info|
|
@@ -100,6 +101,16 @@ class RHash
|
|
100
101
|
# Sorts self.
|
101
102
|
# If block is given using Array#sort else sort by name.
|
102
103
|
#
|
104
|
+
# my_hash = RHash.new
|
105
|
+
#
|
106
|
+
# my_hash["cow"] = "mooo..."
|
107
|
+
#
|
108
|
+
# my_hash["chick"] = "cocó..."
|
109
|
+
#
|
110
|
+
# my_hash["bee"] = "ziiii..."
|
111
|
+
#
|
112
|
+
# my_hash.sort!.inspect #=> {"bee" => "ziiii...", "chick" => "cocó...", "cow" => "mooo..."}
|
113
|
+
#
|
103
114
|
def sort!(&block)
|
104
115
|
sort_hash = int_sort(&block)
|
105
116
|
clear
|
@@ -109,7 +120,7 @@ class RHash
|
|
109
120
|
end
|
110
121
|
|
111
122
|
#
|
112
|
-
#
|
123
|
+
# Orders RHash and calls block once for each key in RHash,
|
113
124
|
# passing the key and value to the block as a two-element array
|
114
125
|
#
|
115
126
|
def sort_each(&block)
|
@@ -119,6 +130,17 @@ class RHash
|
|
119
130
|
end
|
120
131
|
end
|
121
132
|
|
133
|
+
#
|
134
|
+
# Orders RHash and calls block once for each key in RHash,
|
135
|
+
# passing the key and value to the block.
|
136
|
+
#
|
137
|
+
def sort_each_pair(&block)
|
138
|
+
new_hash = int_sort
|
139
|
+
new_hash.each do |info|
|
140
|
+
yield info.key, info.value if block_given?
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
122
144
|
#
|
123
145
|
# See Hash::each
|
124
146
|
#
|
@@ -244,13 +266,6 @@ class RHash
|
|
244
266
|
new_hash
|
245
267
|
end
|
246
268
|
|
247
|
-
#
|
248
|
-
# See RHash::inspect
|
249
|
-
#
|
250
|
-
def to_s
|
251
|
-
inspect
|
252
|
-
end
|
253
|
-
|
254
269
|
#
|
255
270
|
# See Hash::merge
|
256
271
|
#
|
@@ -292,6 +307,13 @@ class RHash
|
|
292
307
|
end
|
293
308
|
end
|
294
309
|
|
310
|
+
#
|
311
|
+
# See Hash::store
|
312
|
+
#
|
313
|
+
def store(key, value)
|
314
|
+
self[key] = value
|
315
|
+
end
|
316
|
+
|
295
317
|
#
|
296
318
|
# Returns an array with first key and value found in RHash.
|
297
319
|
#
|
@@ -304,7 +326,8 @@ class RHash
|
|
304
326
|
# Returns an array with last key and value found in RHash.
|
305
327
|
#
|
306
328
|
def last
|
307
|
-
@hash.last
|
329
|
+
last = @hash.last
|
330
|
+
[last.key, last.value]
|
308
331
|
end
|
309
332
|
|
310
333
|
#
|
@@ -367,10 +390,68 @@ class RHash
|
|
367
390
|
end
|
368
391
|
|
369
392
|
#
|
370
|
-
# See Hash::
|
393
|
+
# See Hash::values_at
|
371
394
|
#
|
372
|
-
def
|
373
|
-
|
395
|
+
def values_at(*keys)
|
396
|
+
selected = []
|
397
|
+
@hash.each do |info|
|
398
|
+
selected << info.value if keys.include? info.key
|
399
|
+
end
|
400
|
+
selected
|
401
|
+
end
|
402
|
+
|
403
|
+
#
|
404
|
+
# See Hash::==
|
405
|
+
#
|
406
|
+
def ==(other)
|
407
|
+
if other.is_a? Hash
|
408
|
+
return equal(other)
|
409
|
+
end
|
410
|
+
|
411
|
+
if other.is_a? RHash
|
412
|
+
return equal(other)
|
413
|
+
end
|
414
|
+
false
|
415
|
+
end
|
416
|
+
|
417
|
+
#
|
418
|
+
# Returns two arrays with difference between RHash and Hash/RHash.
|
419
|
+
# Each array contains different keys and values have found.
|
420
|
+
# The first array is related to whom calls the method
|
421
|
+
# and the second array referring to those who want to diff.
|
422
|
+
#
|
423
|
+
# n = RHash.new
|
424
|
+
#
|
425
|
+
# n["r"] = 5
|
426
|
+
#
|
427
|
+
# n["s"] = 6
|
428
|
+
#
|
429
|
+
# n["d"] = 5
|
430
|
+
#
|
431
|
+
# n[:h] = "ola"
|
432
|
+
#
|
433
|
+
# n[:m] = 5
|
434
|
+
#
|
435
|
+
# x= RHash.new
|
436
|
+
#
|
437
|
+
# x["r"] = 5
|
438
|
+
#
|
439
|
+
# x["s"] = 6
|
440
|
+
#
|
441
|
+
# x["d"] = 5
|
442
|
+
#
|
443
|
+
# x["t"] = 5
|
444
|
+
#
|
445
|
+
# n.diff x #=> [[:h, "ola"], [:m, 5]], [["t", 5]]
|
446
|
+
#
|
447
|
+
def diff(other)
|
448
|
+
if other.is_a? Hash
|
449
|
+
return int_diff(other)
|
450
|
+
end
|
451
|
+
|
452
|
+
if other.is_a? RHash
|
453
|
+
return int_diff(other)
|
454
|
+
end
|
374
455
|
end
|
375
456
|
|
376
457
|
#
|
@@ -385,15 +466,24 @@ class RHash
|
|
385
466
|
end
|
386
467
|
|
387
468
|
#
|
388
|
-
#
|
469
|
+
# Return the contents of this hash as a string.
|
389
470
|
#
|
390
|
-
def
|
391
|
-
|
392
|
-
@hash.
|
393
|
-
|
471
|
+
def inspect
|
472
|
+
msg = '{'
|
473
|
+
@hash.each_with_index do |info, index|
|
474
|
+
msg += info.to_s
|
475
|
+
msg += ", " if @hash.size > 1 and index + 1 < @hash.size
|
394
476
|
end
|
395
|
-
|
477
|
+
msg += '}'
|
478
|
+
msg
|
396
479
|
end
|
480
|
+
|
481
|
+
#
|
482
|
+
# See RHash::inspect
|
483
|
+
#
|
484
|
+
def to_s
|
485
|
+
inspect
|
486
|
+
end
|
397
487
|
|
398
488
|
#
|
399
489
|
# Converts to a Hash.
|
@@ -408,6 +498,44 @@ class RHash
|
|
408
498
|
|
409
499
|
private
|
410
500
|
|
501
|
+
def equal(other)
|
502
|
+
if other.size == self.size
|
503
|
+
hits = other.size
|
504
|
+
self.each_pair do |key, value|
|
505
|
+
other.each_pair do |k,v|
|
506
|
+
hits = hits - 1 if (key == k) and (value == v)
|
507
|
+
end
|
508
|
+
end
|
509
|
+
return (hits == 0)? true : false
|
510
|
+
else
|
511
|
+
return false
|
512
|
+
end
|
513
|
+
end
|
514
|
+
|
515
|
+
def int_diff(other)
|
516
|
+
my_diff = []
|
517
|
+
other_diff = []
|
518
|
+
is_member = false
|
519
|
+
self.each_pair do |key, value|
|
520
|
+
other.each_pair do |k,v|
|
521
|
+
is_member = true if (key == k) and (value == v)
|
522
|
+
end
|
523
|
+
my_diff << [key, value] unless is_member
|
524
|
+
is_member = false
|
525
|
+
end
|
526
|
+
|
527
|
+
is_member = false
|
528
|
+
other.each_pair do |key, value|
|
529
|
+
self.each_pair do |k,v|
|
530
|
+
is_member = true if (key == k) and (value == v)
|
531
|
+
end
|
532
|
+
other_diff << [key, value] unless is_member
|
533
|
+
is_member = false
|
534
|
+
end
|
535
|
+
|
536
|
+
return my_diff, other_diff
|
537
|
+
end
|
538
|
+
|
411
539
|
def int_sort(&block)
|
412
540
|
new_hash = []
|
413
541
|
if !block_given?
|
data/test/test.rb
CHANGED
@@ -1,75 +1,27 @@
|
|
1
1
|
require 'lib/rhash'
|
2
|
-
|
2
|
+
require 'rubygems'
|
3
|
+
#require 'test/unit'
|
3
4
|
n = RHash.new
|
4
|
-
n[
|
5
|
-
n[
|
6
|
-
n["
|
7
|
-
n[
|
8
|
-
n[
|
5
|
+
n["r"] = 5
|
6
|
+
n["s"] = 6
|
7
|
+
n["d"] = 5
|
8
|
+
n[:h] = "ola"
|
9
|
+
n[:m] = 5
|
10
|
+
|
11
|
+
#x= {}
|
12
|
+
#x["s"] = 5
|
13
|
+
#x["r"] = 6
|
14
|
+
|
15
|
+
x= RHash.new
|
16
|
+
x["r"] = 5
|
17
|
+
x["s"] = 6
|
18
|
+
x["d"] = 5
|
19
|
+
x["t"] = 5
|
20
|
+
|
21
|
+
#puts n == x
|
22
|
+
#puts n == 5
|
9
23
|
|
10
|
-
|
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]
|
18
|
-
#n.sort_each{|i| puts i}
|
19
|
-
#n.each{|i| puts i}
|
20
|
-
#n.each_pair{|i, j| puts i; puts j+1}
|
21
|
-
#n.each_value{|i| puts i+10}
|
22
|
-
#n.each_key{|i| puts i.inspect}
|
23
|
-
puts n.select{|i| i.value > 100}
|
24
|
-
#puts n.keys.inspect
|
25
|
-
#puts n.values.inspect
|
26
|
-
#puts n.has_key?("a")
|
27
|
-
#puts n.has_key?(:a)
|
28
|
-
#puts n.has_value?(50)
|
29
|
-
#puts n.has_value?(105)
|
30
|
-
#n.delete_if{|i, j| i == :g}
|
31
|
-
#puts n.inspect
|
32
|
-
#puts n.delete(:gh){|i| puts "Key #{i} not found"}
|
33
|
-
#puts n.inspect
|
34
|
-
#puts n.invert
|
35
|
-
#puts n.length
|
36
|
-
#puts n.size
|
37
|
-
#m = RHash.new
|
38
|
-
#m[:h] = :garrafa
|
39
|
-
#m[:b] = "a"
|
40
|
-
#m = {:h => "garrafas", :b => :a}
|
41
|
-
#n.merge! m
|
42
|
-
#x = n.merge m
|
43
|
-
#puts "Hash pós merge"
|
44
|
-
#puts x.inspect
|
45
|
-
#puts "Hash original"
|
46
|
-
#puts n.inspect
|
47
|
-
#puts n.inspect
|
48
|
-
#puts n.first
|
49
|
-
#puts n.last
|
50
|
-
#puts n.fetch(:hhh){"hahahahaha"}
|
51
|
-
#n.clear
|
52
|
-
#puts n.inspect
|
53
|
-
#h = RHash.new
|
54
|
-
#h.default = "5"
|
55
|
-
#h.default(5)
|
56
|
-
#puts h.inspect
|
57
|
-
#h.default= Proc.new {|h, k| h[k] = k.to_i*10}
|
58
|
-
#h.default(5)
|
59
|
-
#h.default(10)
|
60
|
-
#puts h.inspect
|
61
|
-
#h = RHash.new{|h, k| h[k] = k.to_i*10}
|
62
|
-
#h.default(5)
|
63
|
-
#h.default(10)
|
64
|
-
#puts h.inspect
|
65
|
-
#puts n.inspect
|
66
|
-
#n.shift
|
67
|
-
#puts n.inspect
|
68
|
-
#n.store(666, 0)
|
69
|
-
#puts n.inspect
|
70
|
-
#puts n.to_a.inspect
|
71
|
-
#puts n.empty?
|
72
|
-
#m = RHash.new
|
73
|
-
#puts m.empty?
|
74
|
-
#puts n.values_at(:a, :xxx).inspect
|
24
|
+
a,b = n.diff x
|
75
25
|
|
26
|
+
puts a.inspect
|
27
|
+
puts b.inspect
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rhash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Rodrigo Mello Nardi
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-07-
|
18
|
+
date: 2010-07-15 00:00:00 -03:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|