LoginSignup
8
7

More than 5 years have passed since last update.

EmacsがRubyに差し込むMagic Commentを一掃する

Posted at

Ruby2.0以降.rbファイルのデフォルトのエンコーディングがutf-8になったので以下のようなマジックコメントはいらなくなりました。

user.rb
# -- coding: utf-8 -*-
class User < ActiveRecord::Base
  # ...
end

まずEmacs側で再発するのを防ぎます。
Emacsのruby-modeでマジックコメントの自動挿入を停止する - Qiita

init.el
(custom-set-variables
 '(ruby-insert-encoding-magic-comment nil))

次に既存のコードベースを綺麗にします。
sedで掃除をしようとすると、改行を取り除く構文がしんどいのでPerlを使いました。
Perl便利ですね。

find . -type d ( -name "vendor" -o -name "tmp" -o -name "public" )\
 -prune -o -type f -name '.rb' | xargs perl -pi -e "s/# -\- coding: utf-8 -*-\n//g"

こんな感じで一掃しました。Rubocopにも設定を加えておくとなお良い。
これでautocorrectも効くようになる。

.rubocop.yml
  Style/Encoding:
    Enabled: true
    EnforcedStyle: never
8
7
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
8
7