teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

10

修正

2017/07/23 10:58

投稿

退会済みユーザー
answer CHANGED
@@ -116,7 +116,7 @@
116
116
  input.each do |k, v|
117
117
  self.process(k, v)
118
118
  end
119
- @result.uniq # あやしい
119
+ @result.uniq # 同名のキーと値がるとuniqされるので注意
120
120
  end
121
121
  end
122
122
 

9

修正

2017/07/23 10:58

投稿

退会済みユーザー
answer CHANGED
@@ -70,7 +70,6 @@
70
70
  このプログラムが正確に動くかどうかは分かりません。
71
71
 
72
72
  #追記
73
- 7層まで試しました。
74
73
  配列の中にHashを入れたものに対応したバージョン。
75
74
  hashはotn様のものを使わせていただきました。
76
75
  ```Ruby

8

修正

2017/07/23 10:53

投稿

退会済みユーザー
answer CHANGED
@@ -76,11 +76,13 @@
76
76
  ```Ruby
77
77
  hash = {"test_key1"=>"test_value1",
78
78
  "test_key2"=>[
79
- {"test_key3"=>"test_value3"},
79
+ {"test_key3"=>"test_value3"},
80
- {"test_key4"=>"test_value4",
80
+ {"test_key4"=>"test_value4",
81
+ "test_key5"=>[
81
- "test_key5"=>[{"test_key6"=>"test_value6"},
82
+ {"test_key6"=>"test_value6"},
82
- {"test_key7"=>"test_value7"}]
83
+ {"test_key7"=>"test_value7"}
84
+ ]
83
- }
85
+ }
84
86
  ]
85
87
  }
86
88
 

7

修正

2017/07/23 10:19

投稿

退会済みユーザー
answer CHANGED
@@ -101,8 +101,7 @@
101
101
  elsif b.is_a?(Array)
102
102
  b.each do |e|
103
103
  if e.is_a?(Hash)
104
- _t = self.flat_hash(e)
104
+ @result.concat(self.flat_hash(e))
105
- @result.concat(_t)
106
105
  else
107
106
  @result << [a, b]
108
107
  end

6

修正

2017/07/23 10:17

投稿

退会済みユーザー
answer CHANGED
@@ -101,8 +101,8 @@
101
101
  elsif b.is_a?(Array)
102
102
  b.each do |e|
103
103
  if e.is_a?(Hash)
104
- @result.concat(self.flat_hash(e))
104
+ _t = self.flat_hash(e)
105
- @result.uniq! # あやしい。
105
+ @result.concat(_t)
106
106
  else
107
107
  @result << [a, b]
108
108
  end
@@ -116,7 +116,7 @@
116
116
  input.each do |k, v|
117
117
  self.process(k, v)
118
118
  end
119
- @result
119
+ @result.uniq # あやしい。
120
120
  end
121
121
  end
122
122
 

5

修正

2017/07/23 10:16

投稿

退会済みユーザー
answer CHANGED
@@ -75,11 +75,13 @@
75
75
  hashはotn様のものを使わせていただきました。
76
76
  ```Ruby
77
77
  hash = {"test_key1"=>"test_value1",
78
+ "test_key2"=>[
78
- "test_key2"=>[{"test_key3"=>"test_value3"},
79
+ {"test_key3"=>"test_value3"},
79
80
  {"test_key4"=>"test_value4",
80
81
  "test_key5"=>[{"test_key6"=>"test_value6"},
81
82
  {"test_key7"=>"test_value7"}]
82
- }]
83
+ }
84
+ ]
83
85
  }
84
86
 
85
87
  class Hash

4

修正

2017/07/23 09:59

投稿

退会済みユーザー
answer CHANGED
@@ -76,7 +76,11 @@
76
76
  ```Ruby
77
77
  hash = {"test_key1"=>"test_value1",
78
78
  "test_key2"=>[{"test_key3"=>"test_value3"},
79
+ {"test_key4"=>"test_value4",
80
+ "test_key5"=>[{"test_key6"=>"test_value6"},
79
- {"test_key4"=>"test_value4"}]}
81
+ {"test_key7"=>"test_value7"}]
82
+ }]
83
+ }
80
84
 
81
85
  class Hash
82
86
  def h_flat
@@ -124,4 +128,8 @@
124
128
  test_value3
125
129
  test_key4
126
130
  test_value4
131
+ test_key6
132
+ test_value6
133
+ test_key7
134
+ test_value7
127
135
  ```

3

修正

2017/07/23 09:58

投稿

退会済みユーザー
answer CHANGED
@@ -71,8 +71,13 @@
71
71
 
72
72
  #追記
73
73
  7層まで試しました。
74
- クラスバージョン。
74
+ 配列の中にHashを入れたものに対応したバージョン。
75
+ hashはotn様のものを使わせていただきました。
75
76
  ```Ruby
77
+ hash = {"test_key1"=>"test_value1",
78
+ "test_key2"=>[{"test_key3"=>"test_value3"},
79
+ {"test_key4"=>"test_value4"}]}
80
+
76
81
  class Hash
77
82
  def h_flat
78
83
  F2H.flat_hash(self)
@@ -87,6 +92,15 @@
87
92
  b.each do |k, v|
88
93
  self.process(k, v)
89
94
  end
95
+ elsif b.is_a?(Array)
96
+ b.each do |e|
97
+ if e.is_a?(Hash)
98
+ @result.concat(self.flat_hash(e))
99
+ @result.uniq! # あやしい。
100
+ else
101
+ @result << [a, b]
102
+ end
103
+ end
90
104
  else
91
105
  @result << [a, b]
92
106
  end
@@ -100,7 +114,14 @@
100
114
  end
101
115
  end
102
116
 
117
+ puts hash.h_flat
103
118
 
119
+ ```
120
+ ```ここに言語を入力
104
- p hash.h_flat
121
+ test_key1
105
-
122
+ test_value1
123
+ test_key3
124
+ test_value3
125
+ test_key4
126
+ test_value4
106
127
  ```

2

修正

2017/07/23 09:54

投稿

退会済みユーザー
answer CHANGED
@@ -70,4 +70,37 @@
70
70
  このプログラムが正確に動くかどうかは分かりません。
71
71
 
72
72
  #追記
73
- 7層まで試しました。
73
+ 7層まで試しました。
74
+ クラスバージョン。
75
+ ```Ruby
76
+ class Hash
77
+ def h_flat
78
+ F2H.flat_hash(self)
79
+ end
80
+ end
81
+
82
+ class F2H
83
+ @result = []
84
+
85
+ def self.process(a, b)
86
+ if b.is_a?(Hash)
87
+ b.each do |k, v|
88
+ self.process(k, v)
89
+ end
90
+ else
91
+ @result << [a, b]
92
+ end
93
+ end
94
+
95
+ def self.flat_hash(input)
96
+ input.each do |k, v|
97
+ self.process(k, v)
98
+ end
99
+ @result
100
+ end
101
+ end
102
+
103
+
104
+ p hash.h_flat
105
+
106
+ ```

1

修正

2017/07/23 09:33

投稿

退会済みユーザー
answer CHANGED
@@ -7,7 +7,22 @@
7
7
  "test_key3"=>"test_value3",
8
8
  "test_key4"=>{
9
9
  "test_key5"=>"test_value5",
10
- "test_key6"=>[0, 1, 2, 3]
10
+ "test_key6"=>[0, 1, 2, 3],
11
+ "test_key7"=>{
12
+ "test_key8"=>"test_value8",
13
+ "test_key9"=>"test_value9",
14
+ "test_key10"=>"test_value10",
15
+ "test_key11"=>{
16
+ "test_key12"=>"test_value12",
17
+ "test_key13"=>{
18
+ "test_key14"=>"test_value14",
19
+ "test_key15"=>{
20
+ "test_key16"=>["Hello,World"],
21
+ "test_key17"=>"END"
22
+ }
23
+ }
24
+ }
25
+ }
11
26
  }
12
27
  }
13
28
  }
@@ -25,7 +40,6 @@
25
40
  hash.each do |k, v|
26
41
  test(k, v)
27
42
  end
28
-
29
43
  ```
30
44
  出力結果例
31
45
  ```Ruby
@@ -36,7 +50,24 @@
36
50
  "test_key5"
37
51
  "test_value5"
38
52
  "test_key6"
39
- [0, 1, 2, 3]"
53
+ [0, 1, 2, 3]
54
+ "test_key8"
55
+ "test_value8"
56
+ "test_key9"
57
+ "test_value9"
58
+ "test_key10"
59
+ "test_value10"
60
+ "test_key12"
61
+ "test_value12"
62
+ "test_key14"
63
+ "test_value14"
64
+ "test_key16"
65
+ ["Hello,World"]
66
+ "test_key17"
67
+ "END"
40
68
  ```
41
69
  test_key4にtest_key5とtest_key6を追加して作ってみました。
42
- このプログラムが正確に動くかどうかは分かりません。
70
+ このプログラムが正確に動くかどうかは分かりません。
71
+
72
+ #追記
73
+ 7層まで試しました。