rhash 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/rhash.rb +48 -2
  2. data/test/test.rb +10 -1
  3. metadata +4 -4
@@ -92,7 +92,7 @@ class RHash
92
92
  def sort(&block)
93
93
  sort_rhash = RHash.new
94
94
  int_sort(&block).each do |info|
95
- sort_rhash[info[0]] = info[1]
95
+ sort_rhash[info.key] = info.value
96
96
  end
97
97
  sort_rhash
98
98
  end
@@ -115,7 +115,7 @@ class RHash
115
115
  sort_hash = int_sort(&block)
116
116
  clear
117
117
  sort_hash.each do |info|
118
- self[info[0]] = info[1]
118
+ self[info.key] = info.value
119
119
  end
120
120
  end
121
121
 
@@ -500,6 +500,10 @@ class RHash
500
500
  end
501
501
  alias :to_html :to_xml
502
502
 
503
+ def to_yaml
504
+ yamlfy()
505
+ end
506
+
503
507
  #
504
508
  # See Hash::to_a
505
509
  #
@@ -544,6 +548,48 @@ class RHash
544
548
 
545
549
  private
546
550
 
551
+ def yamlfy
552
+ yaml = ''
553
+ whitespace = " "
554
+ self.each_pair do |key, value|
555
+ if value.is_a? Hash or value.is_a? RHash
556
+ yaml += "#{key}:\n"
557
+ value.each_pair do |sub_key, sub_value|
558
+ if sub_value.is_a? RHash
559
+ yaml += whitespace + "#{sub_key}: \n"
560
+ subwhitespace = whitespace + " "
561
+ yaml += recursive_yaml(sub_value, subwhitespace)
562
+ else
563
+ if sub_value.is_a? Hash
564
+ yaml += whitespace + "#{sub_key}: \n"
565
+ subwhitespace = whitespace + " "
566
+ yaml += recursive_yaml(sub_value, subwhitespace)
567
+ else
568
+ yaml += whitespace+"#{sub_key}: #{sub_value}\n"
569
+ end
570
+ end
571
+ end
572
+ else
573
+ yaml += "#{key}: #{value}\n"
574
+ end
575
+ end
576
+ yaml
577
+ end
578
+
579
+ def recursive_yaml(info,whitespace='')
580
+ info_str = ''
581
+ info.each_pair do |key, value|
582
+ if value.is_a? Hash or value.is_a? RHash
583
+ info_str += whitespace + "#{key}:\n"
584
+ whitespace = whitespace + " "
585
+ info_str += recursive_yaml(value, whitespace)
586
+ else
587
+ info_str += whitespace + "#{key}: #{value}\n"
588
+ end
589
+ end
590
+ info_str
591
+ end
592
+
547
593
  def xml(info)
548
594
  msg_xml = ""
549
595
  if info.is_a? RHash
@@ -14,15 +14,24 @@ n[:m] = 5
14
14
 
15
15
  x= RHash.new
16
16
  x["body"] = RHash.new
17
+ x["xx"] = {:x => {:y => {:z => 666, :g => "galinha"} }}
17
18
  x["body"]["color"] = "0x0454"
18
19
  x["body"]["size"] = 24
19
20
  x["body"]["letter"] = "Arial"
20
21
  x["body"]["mode"] = RHash.new
21
22
  x["body"]["mode"]["present"] = "Love"
22
23
  x["body"]["mode"]["to"] = "Rose Mary"
24
+ x["body"]["hash"] = {:ola => 45}
25
+ x["body"]["mode"]["vaca"] = RHash.new
26
+ x["body"]["mode"]["vaca"]["mooo"] = RHash.new
27
+ x["body"]["mode"]["vaca"]["mooo"]["milk"] = true
28
+ x["body"]["mode"]["vaca"]["mooo"]["feed"] = "grass"
29
+
23
30
  #puts n == x
24
31
  #puts n == 5
25
32
  #a,b = n.diff x
26
33
  #puts a.inspect
27
34
  #puts b.inspect
28
- puts x.to_html
35
+ #puts x.to_html
36
+
37
+ puts x.to_yaml
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: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
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-16 00:00:00 -03:00
18
+ date: 2010-07-19 00:00:00 -03:00
19
19
  default_executable:
20
20
  dependencies: []
21
21