質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

解決済

1回答

2524閲覧

requireの一部のみLoadErrorが発生する

Kampachi_Percy

総合スコア21

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2020/04/30 03:28

前提・実現したいこと

rubypracticeというフォルダの中に、

  • Gemfile
  • Gemfile.lock
  • vendor

などRubyで使うものを入れてあります。
Twitterと連携したものを作ろうと思い、Gemfileに

Gemfile

1gem "twitter" 2gem "json"

と記入し、

bundle install

しました。その後、プログラムを組んで実行したところ、twitterのみLoadErrorが発生しました。
同じタイミングでbundle installしても、何らかの違いが生まれるのでしょうか。
見落としなどありましたらご指摘ください。

問題の確認

Interactive Rubyを用いて対話的に確認しました。

$ irb irb(main):001:0> require 'json' => true irb(main):002:0> require 'twitter' Traceback (most recent call last): 4: from /usr/bin/irb:11:in `<main>' 3: from (irb):2 2: from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require' 1: from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require' LoadError (cannot load such file -- twitter)

kernel_require.rbです(参考になるかどうかわかりませんが一応記載します)

# frozen_string_literal: true #-- # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others. # All rights reserved. # See LICENSE.txt for permissions. #++ require 'monitor' module Kernel RUBYGEMS_ACTIVATION_MONITOR = Monitor.new # :nodoc: if defined?(gem_original_require) then # Ruby ships with a custom_require, override its require remove_method :require else ## # The Kernel#require from before RubyGems was loaded. alias gem_original_require require private :gem_original_require end ## # When RubyGems is required, Kernel#require is replaced with our own which # is capable of loading gems on demand. # # When you call <tt>require 'x'</tt>, this is what happens: # * If the file can be loaded from the existing Ruby loadpath, it # is. # * Otherwise, installed gems are searched for a file that matches. # If it's found in gem 'y', that gem is activated (added to the # loadpath). # # The normal <tt>require</tt> functionality of returning false if # that file has already been loaded is preserved. def require path RUBYGEMS_ACTIVATION_MONITOR.enter path = path.to_path if path.respond_to? :to_path if spec = Gem.find_unresolved_default_spec(path) Gem.remove_unresolved_default_spec(spec) begin Kernel.send(:gem, spec.name) rescue Exception RUBYGEMS_ACTIVATION_MONITOR.exit raise end end # If there are no unresolved deps, then we can use just try # normal require handle loading a gem from the rescue below. if Gem::Specification.unresolved_deps.empty? then RUBYGEMS_ACTIVATION_MONITOR.exit return gem_original_require(path) end # If +path+ is for a gem that has already been loaded, don't # bother trying to find it in an unresolved gem, just go straight # to normal require. #-- # TODO request access to the C implementation of this to speed up RubyGems if Gem::Specification.find_active_stub_by_path(path) RUBYGEMS_ACTIVATION_MONITOR.exit return gem_original_require(path) end # Attempt to find +path+ in any unresolved gems... found_specs = Gem::Specification.find_in_unresolved path # If there are no directly unresolved gems, then try and find +path+ # in any gems that are available via the currently unresolved gems. # For example, given: # # a => b => c => d # # If a and b are currently active with c being unresolved and d.rb is # requested, then find_in_unresolved_tree will find d.rb in d because # it's a dependency of c. # if found_specs.empty? then found_specs = Gem::Specification.find_in_unresolved_tree path found_specs.each do |found_spec| found_spec.activate end # We found +path+ directly in an unresolved gem. Now we figure out, of # the possible found specs, which one we should activate. else # Check that all the found specs are just different # versions of the same gem names = found_specs.map(&:name).uniq if names.size > 1 then RUBYGEMS_ACTIVATION_MONITOR.exit raise Gem::LoadError, "#{path} found in multiple gems: #{names.join ', '}" end # Ok, now find a gem that has no conflicts, starting # at the highest version. valid = found_specs.find { |s| !s.has_conflicts? } unless valid then le = Gem::LoadError.new "unable to find a version of '#{names.first}' to activate" le.name = names.first RUBYGEMS_ACTIVATION_MONITOR.exit raise le end valid.activate end RUBYGEMS_ACTIVATION_MONITOR.exit return gem_original_require(path) rescue LoadError => load_error RUBYGEMS_ACTIVATION_MONITOR.enter begin if load_error.message.start_with?("Could not find") or (load_error.message.end_with?(path) and Gem.try_activate(path)) then require_again = true end ensure RUBYGEMS_ACTIVATION_MONITOR.exit end return gem_original_require(path) if require_again raise load_error end private :require end

バージョン情報

  • ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]
  • json (2.3.0)
  • twitter (7.0.0)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Interactive Rubyを用いて対話的に確認しました。

単にirbで立ち上げると、Bundler経由で入れたGemを使いませんjsonは標準添付なので読み込み可能です。

bundle exec irbのようにして、Bundler環境下で実行してください。

投稿2020/04/30 03:35

maisumakun

総合スコア145201

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Kampachi_Percy

2020/04/30 03:50

仰るとおりでした! プログラムも問題なく実行できています、ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問