はじめに
Railsを使うにあたり、当たり前のようにpuma
とかwebpacker
とかlisten
を使用していますが、正直あまり意味がわからずに使っていました。ここいらで主なGemについてまとめたいと思います。
Railsデフォルトの主なGem
下記がRails6で生成されるデフォルトGemです。
Gemfile
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.3'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '~> 1.4'
# Use Puma as the app server
gem 'puma', '~> 4.1'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 4.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '~> 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
# Easy installation and use of web drivers to run system tests with browsers
gem 'webdrivers'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
```
それぞれ説明していきます。
### sqlite3
SQLデータベースエンジンを実装するC言語ライブラリ。
パスワード設定がない等のセキュリティ機能がないため、基本的に開発環境、テスト環境で使用し、本番環境では別のデーターベースエンジンを使用する。
https://www.sqlite.org/index.html
### puma
RubyWebアプリケーション用のHTTP1.1サーバー構築に使用する。スレッドプールを使用してリクエストを処理する。
https://puma.io/
https://github.com/puma/puma
### sass-rails
RailsでSass(SCSS)使用できる。
https://github.com/rails/sass-rails
### webpacker
Webアプリケーションで一般的に良く使われるメジャーな設定を、標準で実装してくれるwebpackのラッパー。webpackは、最新のJavaScriptアプリケーション用の静的モジュールバンドラー[^1]。
【主な機能】
・[webpack 4.x.x](https://webpack.js.org/)
・複数のエントリポイント[^2]を使用した自動コード分割
・新しいJavaScript構文(ES6)をブラウザで動くように変換
・React、Vue.js、PostCSSなどのモダンなフレームワークに対する豊富な実績
https://github.com/rails/webpacker
### Jbuilder
Jbuilderは、JSON構造を宣言するためのシンプルなDSL(ドメイン特化言語)を提供。
https://github.com/rails/jbuilder
### turbolinks
Webアプリケーションでのリンクの追跡が高速になる。
https://github.com/turbolinks/turbolinks-classic
### bootsnap
railsの起動時の処理を最適化する(パスとrubyのコンパイル結果をキャッシュ)ことで起動時間を短縮してくれる。
ActiveSupport や YAML もサポートしている。
https://github.com/Shopify/bootsnap/blob/master/README.jp.md
### byebug
デバッグツール。
https://github.com/deivid-rodriguez/byebug
### web-console
View 内でコンソールを立ち上げて、変数や parameter などの状態を見る事の出来るデバック用のライブラリ。エラー箇所のデバッグがしやすくなる。
https://github.com/rails/web-console
### listen
ファイルの変更を検知してそれをフックに何か処理ができる。
【主な機能】
・ファイルの変更、追加、削除を検出
・複数のディレクトリを監視
https://github.com/guard/listen
### spring
Railsアプリケーションのプリローダー[^3]。アプリケーションをバックグラウンドで実行し続けることで開発をスピードアップするため、テスト、移行を実行するたびにアプリケーションを起動する必要がなくなる。
https://github.com/rails/spring
### spring-watcher-listen
Springはファイルシステムをポーリング[^4]するのではなく、Listenを使用してファイルシステムの変更を監視します。
https://github.com/jonleighton/spring-watcher-listen/
### capybara
実際のユーザーがアプリをどのように操作するかをシミュレートすることにより、Webアプリケーションのテストを支援する。
https://github.com/teamcapybara/capybara
https://en.wikipedia.org/wiki/Capybara_(software)
### selenium-webdriver
capybaraではJavaScriptをサポートしていないため、selenium-webdriverでシュミレートする。
https://github.com/SeleniumHQ/selenium/tree/trunk/rb
### webdrivers
webブラウザを外部のソフトウェアから操作したり情報を取得したりできるようにするためのものです。
https://github.com/titusfortner/webdrivers
### tzinfo-data
Windows ではタイムゾーン情報用に使用する。UnixベースのOSではtzinfoからシステムのタイムゾーン情報に直接アクセスできるので使用する必要はない。
https://github.com/tzinfo/tzinfo-data
# それ以外の主なGem
### bcrypt
パスワードを暗号化できる。
https://github.com/codahale/bcrypt-ruby
### devise
Webアプリケーションには必須の、ユーザー認証機能を作ることができる。会員登録用フォームを作成や、メールやFacebook等での認証も実装できる。
https://github.com/heartcombo/devise
### kaminari
ページネーション機能を簡単に追加できる。
https://github.com/kaminari/kaminari
### carrierwave
画像のアップロード機能。
https://github.com/carrierwaveuploader/carrierwave
### active admin
CRUD系の管理画面を作成することができる。
https://github.com/activeadmin/activeadmin
### ruboCop
コーディング規約どおりに書かれているかをチェックする静的コード解析ツール。
https://github.com/rubocop-hq/rubocop
### pry-rails
Rubyのirbのようにrailsのコンソールでメソッドなどを使えることができるようになる。
https://github.com/rweng/pry-rails
### faker
偽のデータを生成する。
https://github.com/faker-ruby/faker
### capistrano
自動デプロイツール。
https://github.com/capistrano/capistrano
# まとめ
Railsは歴史が長い分、Gemは数が多いですね。初学者だと選定が難しそうです。まずは開発前にどんな機能がどこまで必要なのかピックアップしてGemを選定する必要がありそうです。Gemを選定の参考になりそうなサイトを張っておきます。この記事も有用なGemがあればどんどん更新していこうと思います。
[Gemの探しツール](https://www.ruby-toolbox.com/)
[Gemのランキングサイト](https://bestgems.org/)
[^1]: モジュールごとに分割され、別々になったJavaScriptファイルの依存関係を解決して、1つのファイルにまとめるツール。
[^2]: プログラムを実行を開始する場所のこと。([エントリーポイント - wiki](https://ja.wikipedia.org/wiki/%E3%82%A8%E3%83%B3%E3%83%88%E3%83%AA%E3%83%BC%E3%83%9D%E3%82%A4%E3%83%B3%E3%83%88#:~:text=%E3%82%A8%E3%83%B3%E3%83%88%E3%83%AA%E3%83%BC%E3%83%9D%E3%82%A4%E3%83%B3%E3%83%88%E3%81%A8%E3%81%AF%E3%80%81%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%A0,%E3%81%8C%E3%83%A1%E3%82%A4%E3%83%B3%E3%83%AB%E3%83%BC%E3%83%81%E3%83%B3%E3%81%A7%E3%81%82%E3%82%8B%E3%80%82))
[^3]: サイトを表示する前に予め画像やコンテンツを先読みしてキャッシュする事。(参考→[プリローダーとは](https://www.ikesai.com/words/%E3%83%97%E3%83%AA%E3%83%AD%E3%83%BC%E3%83%80%E3%83%BC))
[^4]: 主に通信などの競合を回避するために、ホスト側が各機器に対して定期的に問い合わせを行い、条件を満たした場合に送受信や各種処理を行うこと。([ポーリング - wiki](https://ja.wikipedia.org/wiki/%E3%83%9D%E3%83%BC%E3%83%AA%E3%83%B3%E3%82%B0_(%E6%83%85%E5%A0%B1)#:~:text=%E3%83%9D%E3%83%BC%E3%83%AA%E3%83%B3%E3%82%B0%EF%BC%88polling%EF%BC%89%E3%81%A8%E3%81%AF%E3%80%81,%E6%96%B9%E5%BC%8F%E3%81%AE%E3%81%93%E3%81%A8%E3%81%A7%E3%81%82%E3%82%8B%E3%80%82))