LoginSignup
1
1

More than 1 year has passed since last update.

Rubyバージョンに合わせて Gemfile.lock を切り替える

Last updated at Posted at 2022-08-06

目次

  1. Gemfile で Ruby バージョンごとに分岐させる
  2. Ruby のバージョンごとの Gemfile.lock を準備する
  3. .gitignore で Gemfile.lock を除外する

1. Gemfile で Ruby バージョンごとに分岐させる

Gemfile に下記のような形式で書く
各バージョンに合わせて RUBY_VERSION == "3.0.2" は書き換えてください

# coding: utf-8
source 'https://rubygems.org'

# バージョン判定
use_ruby3 = RUBY_VERSION == "3.0.2"
gemfile_path = "#{__dir__}/Gemfile.lock"

# バージョンに一致した Lock ファイルをコピーする
unless File.exist?(gemfile_path)
	if use_ruby3
		FileUtils.copy "#{__dir__}/Gemfile3.lock", gemfile_path
	else
		FileUtils.copy "#{__dir__}/Gemfile23.lock", gemfile_path
	end
end

# バージョンで分岐させるライブラリ
if use_ruby3
	gem 'activesupport', '<=6.1.4'
else
	gem 'activesupport', '<=4.2.4'
end

# 共通のライブラリ
gem 'rake'

2. Ruby のバージョンごとの Gemfile.lock を準備する

Ruby を切り替え bundle を実行し、それぞれの Gemfile.lock を作成する
↓ ファイルの配置イメージ。Gemfile3.lock は Ruby3.0.0、Gemfile23.lock は Ruby2.3.3 のようになっている
image.png

3. .gitignore で Gemfile.lock を除外する

.gitignore に Gemfile.lock を追加する
※ Gemfile に書いた処理によって、それぞれのPCの環境に沿った Gemfile.lock が作られるため

1
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
1
1