回答編集履歴

10

修正

2017/07/23 10:58

投稿

退会済みユーザー
test CHANGED
@@ -234,7 +234,7 @@
234
234
 
235
235
  end
236
236
 
237
- @result.uniq # あやしい
237
+ @result.uniq # 同名のキーと値がるとuniqされるので注意
238
238
 
239
239
  end
240
240
 

9

修正

2017/07/23 10:58

投稿

退会済みユーザー
test CHANGED
@@ -142,8 +142,6 @@
142
142
 
143
143
  #追記
144
144
 
145
- 7層まで試しました。
146
-
147
145
  配列の中にHashを入れたものに対応したバージョン。
148
146
 
149
147
  hashはotn様のものを使わせていただきました。

8

修正

2017/07/23 10:53

投稿

退会済みユーザー
test CHANGED
@@ -154,15 +154,19 @@
154
154
 
155
155
  "test_key2"=>[
156
156
 
157
- {"test_key3"=>"test_value3"},
157
+ {"test_key3"=>"test_value3"},
158
-
158
+
159
- {"test_key4"=>"test_value4",
159
+ {"test_key4"=>"test_value4",
160
+
160
-
161
+ "test_key5"=>[
162
+
161
- "test_key5"=>[{"test_key6"=>"test_value6"},
163
+ {"test_key6"=>"test_value6"},
162
-
164
+
163
- {"test_key7"=>"test_value7"}]
165
+ {"test_key7"=>"test_value7"}
166
+
164
-
167
+ ]
168
+
165
- }
169
+ }
166
170
 
167
171
  ]
168
172
 

7

修正

2017/07/23 10:19

投稿

退会済みユーザー
test CHANGED
@@ -204,9 +204,7 @@
204
204
 
205
205
  if e.is_a?(Hash)
206
206
 
207
- _t = self.flat_hash(e)
207
+ @result.concat(self.flat_hash(e))
208
-
209
- @result.concat(_t)
210
208
 
211
209
  else
212
210
 

6

修正

2017/07/23 10:17

投稿

退会済みユーザー
test CHANGED
@@ -204,9 +204,9 @@
204
204
 
205
205
  if e.is_a?(Hash)
206
206
 
207
- @result.concat(self.flat_hash(e))
207
+ _t = self.flat_hash(e)
208
-
208
+
209
- @result.uniq! # あやしい。
209
+ @result.concat(_t)
210
210
 
211
211
  else
212
212
 
@@ -234,7 +234,7 @@
234
234
 
235
235
  end
236
236
 
237
- @result
237
+ @result.uniq # あやしい。
238
238
 
239
239
  end
240
240
 

5

修正

2017/07/23 10:16

投稿

退会済みユーザー
test CHANGED
@@ -152,7 +152,9 @@
152
152
 
153
153
  hash = {"test_key1"=>"test_value1",
154
154
 
155
+ "test_key2"=>[
156
+
155
- "test_key2"=>[{"test_key3"=>"test_value3"},
157
+ {"test_key3"=>"test_value3"},
156
158
 
157
159
  {"test_key4"=>"test_value4",
158
160
 
@@ -160,7 +162,9 @@
160
162
 
161
163
  {"test_key7"=>"test_value7"}]
162
164
 
163
- }]
165
+ }
166
+
167
+ ]
164
168
 
165
169
  }
166
170
 

4

修正

2017/07/23 09:59

投稿

退会済みユーザー
test CHANGED
@@ -154,7 +154,15 @@
154
154
 
155
155
  "test_key2"=>[{"test_key3"=>"test_value3"},
156
156
 
157
+ {"test_key4"=>"test_value4",
158
+
159
+ "test_key5"=>[{"test_key6"=>"test_value6"},
160
+
157
- {"test_key4"=>"test_value4"}]}
161
+ {"test_key7"=>"test_value7"}]
162
+
163
+ }]
164
+
165
+ }
158
166
 
159
167
 
160
168
 
@@ -250,4 +258,12 @@
250
258
 
251
259
  test_value4
252
260
 
261
+ test_key6
262
+
263
+ test_value6
264
+
265
+ test_key7
266
+
267
+ test_value7
268
+
253
- ```
269
+ ```

3

修正

2017/07/23 09:58

投稿

退会済みユーザー
test CHANGED
@@ -144,10 +144,20 @@
144
144
 
145
145
  7層まで試しました。
146
146
 
147
- クラスバージョン。
147
+ 配列の中にHashを入れたものに対応したバージョン。
148
+
149
+ hashはotn様のものを使わせていただきました。
148
150
 
149
151
  ```Ruby
150
152
 
153
+ hash = {"test_key1"=>"test_value1",
154
+
155
+ "test_key2"=>[{"test_key3"=>"test_value3"},
156
+
157
+ {"test_key4"=>"test_value4"}]}
158
+
159
+
160
+
151
161
  class Hash
152
162
 
153
163
  def h_flat
@@ -176,6 +186,24 @@
176
186
 
177
187
  end
178
188
 
189
+ elsif b.is_a?(Array)
190
+
191
+ b.each do |e|
192
+
193
+ if e.is_a?(Hash)
194
+
195
+ @result.concat(self.flat_hash(e))
196
+
197
+ @result.uniq! # あやしい。
198
+
199
+ else
200
+
201
+ @result << [a, b]
202
+
203
+ end
204
+
205
+ end
206
+
179
207
  else
180
208
 
181
209
  @result << [a, b]
@@ -202,10 +230,24 @@
202
230
 
203
231
 
204
232
 
205
-
206
-
207
- p hash.h_flat
233
+ puts hash.h_flat
208
-
209
-
210
-
234
+
235
+
236
+
211
- ```
237
+ ```
238
+
239
+ ```ここに言語を入力
240
+
241
+ test_key1
242
+
243
+ test_value1
244
+
245
+ test_key3
246
+
247
+ test_value3
248
+
249
+ test_key4
250
+
251
+ test_value4
252
+
253
+ ```

2

修正

2017/07/23 09:54

投稿

退会済みユーザー
test CHANGED
@@ -143,3 +143,69 @@
143
143
  #追記
144
144
 
145
145
  7層まで試しました。
146
+
147
+ クラスバージョン。
148
+
149
+ ```Ruby
150
+
151
+ class Hash
152
+
153
+ def h_flat
154
+
155
+ F2H.flat_hash(self)
156
+
157
+ end
158
+
159
+ end
160
+
161
+
162
+
163
+ class F2H
164
+
165
+ @result = []
166
+
167
+
168
+
169
+ def self.process(a, b)
170
+
171
+ if b.is_a?(Hash)
172
+
173
+ b.each do |k, v|
174
+
175
+ self.process(k, v)
176
+
177
+ end
178
+
179
+ else
180
+
181
+ @result << [a, b]
182
+
183
+ end
184
+
185
+ end
186
+
187
+
188
+
189
+ def self.flat_hash(input)
190
+
191
+ input.each do |k, v|
192
+
193
+ self.process(k, v)
194
+
195
+ end
196
+
197
+ @result
198
+
199
+ end
200
+
201
+ end
202
+
203
+
204
+
205
+
206
+
207
+ p hash.h_flat
208
+
209
+
210
+
211
+ ```

1

修正

2017/07/23 09:33

投稿

退会済みユーザー
test CHANGED
@@ -16,7 +16,37 @@
16
16
 
17
17
  "test_key5"=>"test_value5",
18
18
 
19
- "test_key6"=>[0, 1, 2, 3]
19
+ "test_key6"=>[0, 1, 2, 3],
20
+
21
+ "test_key7"=>{
22
+
23
+ "test_key8"=>"test_value8",
24
+
25
+ "test_key9"=>"test_value9",
26
+
27
+ "test_key10"=>"test_value10",
28
+
29
+ "test_key11"=>{
30
+
31
+ "test_key12"=>"test_value12",
32
+
33
+ "test_key13"=>{
34
+
35
+ "test_key14"=>"test_value14",
36
+
37
+ "test_key15"=>{
38
+
39
+ "test_key16"=>["Hello,World"],
40
+
41
+ "test_key17"=>"END"
42
+
43
+ }
44
+
45
+ }
46
+
47
+ }
48
+
49
+ }
20
50
 
21
51
  }
22
52
 
@@ -52,8 +82,6 @@
52
82
 
53
83
  end
54
84
 
55
-
56
-
57
85
  ```
58
86
 
59
87
  出力結果例
@@ -74,10 +102,44 @@
74
102
 
75
103
  "test_key6"
76
104
 
77
- [0, 1, 2, 3]"
105
+ [0, 1, 2, 3]
106
+
107
+ "test_key8"
108
+
109
+ "test_value8"
110
+
111
+ "test_key9"
112
+
113
+ "test_value9"
114
+
115
+ "test_key10"
116
+
117
+ "test_value10"
118
+
119
+ "test_key12"
120
+
121
+ "test_value12"
122
+
123
+ "test_key14"
124
+
125
+ "test_value14"
126
+
127
+ "test_key16"
128
+
129
+ ["Hello,World"]
130
+
131
+ "test_key17"
132
+
133
+ "END"
78
134
 
79
135
  ```
80
136
 
81
137
  test_key4にtest_key5とtest_key6を追加して作ってみました。
82
138
 
83
139
  このプログラムが正確に動くかどうかは分かりません。
140
+
141
+
142
+
143
+ #追記
144
+
145
+ 7層まで試しました。