回答編集履歴
3
修正
answer
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
```Ruby
|
2
2
|
def to_hash(str)
|
3
|
-
str.scan(%r|"(.+?)"=>"(.+?)"|).map{|k, v|[k.to_sym, v
|
3
|
+
str.scan(%r|"(.+?)"=>"(.+?)"|).map{|k, v|[k.to_sym, v]}.to_h
|
4
4
|
end
|
5
5
|
|
6
|
-
hash = to_hash('{"apple"=>"
|
6
|
+
hash = to_hash('{"apple"=>"1", "banana"=>"ばなな"}')
|
7
7
|
|
8
8
|
puts hash[:apple]
|
9
9
|
```
|
2
修正
answer
CHANGED
@@ -10,4 +10,7 @@
|
|
10
10
|
実行結果例
|
11
11
|
```
|
12
12
|
りんご
|
13
|
-
```
|
13
|
+
```
|
14
|
+
質問者様が配列だとおっしゃっているものは
|
15
|
+
配列ではないと思われます。
|
16
|
+
私の勉強不足かもしれませんが文字列ではないかと。
|
1
修正
answer
CHANGED
@@ -3,5 +3,11 @@
|
|
3
3
|
str.scan(%r|"(.+?)"=>"(.+?)"|).map{|k, v|[k.to_sym, v.to_s]}.to_h
|
4
4
|
end
|
5
5
|
|
6
|
-
|
6
|
+
hash = to_hash('{"apple"=>"りんご", "banana"=>"ばなな"}')
|
7
|
+
|
8
|
+
puts hash[:apple]
|
9
|
+
```
|
10
|
+
実行結果例
|
11
|
+
```
|
12
|
+
りんご
|
7
13
|
```
|