Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use read BOM|utf-8 flag for opening files, instead of string replacing #574

Merged
merged 2 commits into from
Apr 20, 2016

Conversation

kmuto
Copy link
Owner

@kmuto kmuto commented Apr 20, 2016

Windows向けに処理。

ちょっと怖いのでドキュメントでテストが必要。

@@ -32,7 +32,7 @@ def initialize(book, number, name, path, io = nil)
@content = nil
end
if !@content && @path && File.exist?(@path)
@content = File.read(@path).sub(/\A\xEF\xBB\xBF/u, '')
@content = File.open(@path, 'r:BOM|utf-8').read
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここはたぶん
@content = File.read(@path, :mode=>'r:BOM|utf-8')
でいけるはずです。というかFile.readを使わずFile.open(...).readすると、openしたファイルが閉じられないのであまりおすすめではないのでした。
@content = File.read(@path, mode: 'r:BOM|utf-8')
でもよいです(rubocopの警告がでなければ、こっちの方が読みやすいのでおすすめ)。

@kmuto
Copy link
Owner Author

kmuto commented Apr 20, 2016

おぉ、modeでいけるのですね。readは:encodingしかないのかと思っていました。

@@ -26,7 +26,7 @@ def initialize(book, number, chapters, name = "", io = nil)
if io
@content = io.read
elsif @path && File.exist?(@path)
@content = File.read(@path).sub(/\A\xEF\xBB\xBF/u, '')
@content = File.read(@path, 'r:BOM|utf-8')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここの引数の書き方は、lib/review/book/chapter.rb でコメントしたような、:modeをキーにしたハッシュにしないとエラーになるはずです(Hashと解釈されない値を第2引数にするとlengthに解釈されて数値じゃないとダメなはず)。

@takahashim
Copy link
Collaborator

ではこれでマージしますね

@takahashim takahashim merged commit 1d81349 into master Apr 20, 2016
@takahashim takahashim deleted the remove_bom_by_openflag branch April 20, 2016 05:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants