0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Rails初学者】Ruby3.3/3.4でhamlit・haml:replace_erbsが動かない問題の解決法

Posted at

Ruby3.3/3.4でhamlit・haml:replace_erbsが動かない問題の解決法

はじめに

Ruby 3.3以降にアップデートした際、hamlit gemのインストールエラーや haml:replace_erbs タスクの実行エラーに直面したため、その際に行った対処方法を備忘録としてまとめます。
間違えていましたら、コメントをいただけますと幸いです。

環境

  • macOS(arm64-darwin)
  • Ruby 3.4.2 → 3.3.5
  • Rails 8
  • rbenv 使用

問題1: hamlit gemのインストールエラー

エラー内容

bundle install

実行時に以下のエラーが発生

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
error: 'stdckdint.h' file not found with <angled> include; use "quotes" instead
   48 | # include <stdckdint.h>
      |           ^~~~~~~~~~~~~
3 warnings and 1 error generated.
make: *** [hamlit.o] Error 1

原因

Ruby3.4系の内部ヘッダ仕様変更にhamlit3.0.3が対応していないため。stdckdint.h の参照方法が変更されたが、hamlitがまだ追従できていない。

解決方法

Rubyバージョンをダウングレードをしました

# Ruby 3.3.5をインストール
rbenv install 3.3.5

# プロジェクトだけ3.3.5を使用
cd ~/my_blog_app
rbenv local 3.3.5

# バージョン確認
ruby -v
# => ruby 3.3.5 (2024-09-24 revision ...) [arm64-darwin]

# Gemを再インストール
bundle install

これでhamlitのビルドエラーが出なくなりました。

問題2: haml:replace_erbs タスクエラー

エラー内容

bundle exec rake haml:replace_erbs

実行時のエラー:

Looking for ERB files to convert to Haml...
rake aborted!
NoMethodError: undefined method `exists?' for module FileTest (NoMethodError)

        unless FileTest.exists?(haml_path)
                       ^^^^^^^^
Did you mean?  exist?

原因

  • Ruby 3.2: FileTest.exists? が非推奨化
  • Ruby 3.3: 完全に削除
  • 古いhaml-railsのタスク内でまだ FileTest.exists? を使用

解決方法

方法1: 互換パッチを追加

Rakefile の先頭に以下を追記

# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

# Ruby 3.3 以降で削除された FileTest.exists? を復活させる互換パッチ
module FileTest
  class << self
    alias_method :exists?, :exist?
  end
end

require_relative "config/application"

Rails.application.load_tasks

方法2: gemアップデート

# haml-rails が Gemfile にない場合は追加
echo "gem 'haml-rails'" >> Gemfile

bundle update haml haml-rails

※新しいバージョンがRuby3.3に対応している場合のみ有効らしい。

方法3: 直接変換コマンドを使用

# 1ファイルずつ変換
bundle exec html2haml app/views/articles/show.html.erb app/views/articles/show.html.haml

実行結果

パッチ適用後、正常に動作しました。

bundle exec rake haml:replace_erbs
Looking for ERB files to convert to Haml...
Converting: app/views/articles/show.html.erb -> app/views/articles/show.html.haml
Converting: app/views/layouts/application.html.erb -> app/views/layouts/application.html.haml
Done!

その他の対処法

hamlit を使わない場合

Gemfileから削除

# gem 'hamlit'  # コメントアウト

標準のhamlやerbを使用する。

Ruby3.4を使い続けたい場合

GitHubの開発ブランチを直接指定する。

gem 'hamlit', github: 'k0kubun/hamlit', branch: 'master'

まとめ

  • hamlit: Ruby3.4は未対応なので3.3系を使用した。
  • haml:replace_erbs: FileTest.exists? 削除による問題は互換パッチを使用する。
  • 安定性重視: プロダクション環境では実績のあるRuby3.3系が推奨みたい。

参考記事


初学者のため、間違えていたらすいません。

0
0
4

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?