Railsアプリケーション初期段階のGemfileに記載されているplatformsについて調べた

rails new .でRailsアプリケーションを作成すると、Gemfileが生成されます。

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.5'

gem 'rails', '~> 6.0.0'
  ~~~~~
gem 'bootsnap', '>= 1.4.2', require: false

group :development, :test do
  ~~~~~
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

  ~~~~~
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]には、オプションとしてplatformsが指定されています。

このオプションが何を表しているのか気になったので調べてみることにしました。

Gemfileとは

RubyのライブラリであるGemの依存関係を管理するためのツールにbundlerがあります。

Gemfileは、bundlerで管理するGemやその管理方法などを記載したファイルです。

公式のドキュメントには、以下のような記載があります。

A Gemfile describes the gem dependencies required to execute associated
Ruby code.

Platforms

platformsは、プラットフォームの種類によってinstallを行うかを指定できます。

bundle platformを実行することで現在のプラットフォームを知ることができます。

gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]のうちmingwx64_mingwはWindows環境のRubyです。mriはrubyの一部です。

ruby
C Ruby (MRI), Rubinius or TruffleRuby, but NOT Windows
mri
Same as ruby, but only C Ruby (MRI)

また、バージョンを指定することやグループ化することが可能です。

gem "ruby-debug", platforms: :mri_18

platforms :ruby do
  gem "ruby-debug"
  gem "sqlite3"
end

その他のオプション

source,git_source,requireについても調べてみました。

sourceは、Rubygemsのリンク先を指定します。

source 'https://rubygems.org'

git_sourceは、Rubygemsに登録されていないGemをinstallをしたいに利用します。ブランチ名も指定できます。 会社独自のGemを利用する際などに使えそうです。

git_source(:github) { |repo| "https://github.com/#{repo}.git" }
git "rails", :github => "rails"

git_source(:stash){ |repo_name| "https://stash.corp.acme.pl/#{repo_name}.git" }
gem "rails", :stash => "forks/rails", :branch => "branch_name"

requireをfalseにすることでRails起動時にautorequiredを行わないように設定できます。

gem 'bootsnap', '>= 1.4.2', require: false