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

回答編集履歴

3

修正

2017/03/17 02:46

投稿

退会済みユーザー
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.to_s]}.to_h
3
+ str.scan(%r|"(.+?)"=>"(.+?)"|).map{|k, v|[k.to_sym, v]}.to_h
4
4
  end
5
5
 
6
- hash = to_hash('{"apple"=>"りんご", "banana"=>"ばなな"}')
6
+ hash = to_hash('{"apple"=>"1", "banana"=>"ばなな"}')
7
7
 
8
8
  puts hash[:apple]
9
9
  ```

2

修正

2017/03/17 02:46

投稿

退会済みユーザー
answer CHANGED
@@ -10,4 +10,7 @@
10
10
  実行結果例
11
11
  ```
12
12
  りんご
13
- ```
13
+ ```
14
+ 質問者様が配列だとおっしゃっているものは
15
+ 配列ではないと思われます。
16
+ 私の勉強不足かもしれませんが文字列ではないかと。

1

修正

2017/03/16 12:23

投稿

退会済みユーザー
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
- puts to_hash('{"apple"=>"りんご", "banana"=>"ばなな"}')
6
+ hash = to_hash('{"apple"=>"りんご", "banana"=>"ばなな"}')
7
+
8
+ puts hash[:apple]
9
+ ```
10
+ 実行結果例
11
+ ```
12
+ りんご
7
13
  ```