LoginSignup
2
1

More than 5 years have passed since last update.

Ruby:ファイルのaccess_modeの指定と文字コードの指定方法備忘録

Posted at

openメソッドでのaccess modeの指定

  • 第二引数がaccess modeの指定になる
  • wを渡すとファイルが存在しない場合は新規作成、存在する場合は上書きする
test.rb
open(file_path, 'w') do |file|
  file.write(output_json)
end

openメソッドでの文字コードの指定

  • 第二引数にてaccess mode:文字コードの形式で文字コードを指定する
test.rb
open(file_path, 'w:utf-8') do |file|
  file.write(output_json)
end

まとめ

  • rubyのversionは多分ですが2.3.1とか(今手元に無い)。
  • 古いドキュメントが結構検索にヒットして、引数が大幅に変わっていたりするのでversionをよく確認した方が良い
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1