回答編集履歴

3

修正

2018/12/21 11:12

投稿

asm
asm

スコア15147

test CHANGED
@@ -42,7 +42,7 @@
42
42
 
43
43
  unless entry.name.valid_encoding?
44
44
 
45
- entry.name.force_encoding case NKF.guess
45
+ entry.name.force_encoding case NKF.guess(entry.name)
46
46
 
47
47
  when NKF::EUC then 'euc-jp'
48
48
 

2

修正

2018/12/21 11:11

投稿

asm
asm

スコア15147

test CHANGED
@@ -36,6 +36,10 @@
36
36
 
37
37
  require 'nkf'
38
38
 
39
+
40
+
41
+
42
+
39
43
  unless entry.name.valid_encoding?
40
44
 
41
45
  entry.name.force_encoding case NKF.guess

1

追記

2018/12/21 10:25

投稿

asm
asm

スコア15147

test CHANGED
@@ -25,3 +25,45 @@
25
25
 
26
26
 
27
27
  [`NKF.guess`](https://docs.ruby-lang.org/ja/latest/method/NKF/m/guess.html)や[charlock_holmes](https://github.com/brianmario/charlock_holmes)を用いる方法があります。
28
+
29
+
30
+
31
+
32
+
33
+ ```rb
34
+
35
+ # NKFを用いた場合
36
+
37
+ require 'nkf'
38
+
39
+ unless entry.name.valid_encoding?
40
+
41
+ entry.name.force_encoding case NKF.guess
42
+
43
+ when NKF::EUC then 'euc-jp'
44
+
45
+ when NKF::SJIS then 'cp932'
46
+
47
+ when NKF::UTF8 then 'utf-8'
48
+
49
+ when NKF::UTF16 then 'utf-16'
50
+
51
+ when Encoding::EUCJP_MS then Encoding::EUCJP_MS
52
+
53
+ when Encoding::CP51932 then Encoding::CP51932
54
+
55
+ when Encoding::WINDOWS_31J then Encoding::WINDOWS_31J
56
+
57
+ else
58
+
59
+ # JISコードもしくは不明
60
+
61
+ raise "文字コードが想定外です"
62
+
63
+ end
64
+
65
+ entry.name.encode! 'utf-8'
66
+
67
+ end
68
+
69
+ ```