In the previous article, I wrote about what I called a shell script with cron and stumbled when running ruby.
Actually, I was stumbling on another one, so I will output it so as not to forget its contents.
About the error that I got when doing Bundler.require
in the ruby file.
-If you run Ruby with crontab, private method `require'called for Bundler: Module
You can cd to the directory where the Gemfile is located and then run ruby. But I don't understand it properly.
/home/user_name/ruby_test.sh
#!/bin/bash
cd /home/user_name/sample && ruby /home/user_name/sample/tasks/test_ruby.rb
crontab
00 13 * * * /bin/bash /home/user_name/ruby_test.sh > /home/user_name/log/ruby_test.log 2>&1
/home/user_name/ruby_test.sh
#!/bin/bash
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
rbenv shell 2.7.1
ruby /home/user_name/sample/tasks/test_ruby.rb
/home/user_name/sample/tasks/test_ruby.rb
require 'bundler/setup'
Bundler.require
#The ruby process continues below
The executed script. Looking at the log file, there is some error. ..
/home/user_name/log/ruby_test.log
/home/user_name/sample/tasks/test_ruby.rb:2:in `<main>': private method `require' called for Bundler:Module (NoMethodError)
It doesn't seem to be working.
But
cd /home/user_name/sample
ruby /tasks/test_ruby.rb
If you execute, you can execute it without any error.
I didn't understand why, but
https://github.com/rubygems/bundler/blob/master/lib/bundler.rb#L173
Bundler.require
I decided to see what he was doing.
bundler/lib/bundler.rb
def require(*groups)
setup(*groups).require(*groups)
end
Let's follow what setup
is.
https://github.com/rubygems/bundler/blob/35be6d9a603084f719fec4f4028c18860def07f6/lib/bundler.rb#L139
bundler/lib/bundler.rb
def setup(*groups)
# Return if all groups are already loaded
return @setup if defined?(@setup) && @setup
definition.validate_runtime!
SharedHelpers.print_major_deprecations!
if groups.empty?
# Load all groups, but only once
@setup = load.setup
else
load.setup(*groups)
end
end
This time I'm calling with no arguments
@ setup = load.setup
is likely to be called.
bundler/lib/bundler.rb
def load
@load ||= Runtime.new(root, definition)
end
bundler/lib/bundler/runtime.rb
def setup(*groups)
@definition.ensure_equivalent_gemfile_and_lockfile if Bundler.frozen_bundle?
groups.map!(&:to_sym)
# Has to happen first
clean_load_path
specs = groups.any? ? @definition.specs_for(groups) : requested_specs
SharedHelpers.set_bundle_environment
Bundler.rubygems.replace_entrypoints(specs)
# Activate the specs
load_paths = specs.map do |spec|
unless spec.loaded_from
raise GemNotFound, "#{spec.full_name} is missing. Run `bundle install` to get it."
end
check_for_activated_spec!(spec)
Bundler.rubygems.mark_loaded(spec)
spec.load_paths.reject {|path| $LOAD_PATH.include?(path) }
end.reverse.flatten
Bundler.rubygems.add_to_load_path(load_paths)
setup_manpath
lock(:preserve_unknown_sections => true)
self
end
I can't call it require in the first place. I realize that looking ahead will only help me study. I get an error saying that it is a private method, but it does not look like a private method. Are you just looking at the wrong place? ?? ?? ?? ??
Note: Bundler is now an archive. It seems better to use rubygems.
I'll read it again when I have time, I still don't understand exactly where the error is being returned and why it happened. .. : disappointed:
/home/user_name/ruby_test.sh
#!/bin/bash
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
rbenv shell 2.7.1
cd /home/user_name/sample && ruby /home/user_name/sample/tasks/test_ruby.rb
I don't understand it properly, but it worked. .. .. : thinking: I don't understand it. Weekend homework. .. I will continue to investigate for the time being. : notebook:: runner:
If you have any kind people, I would be grateful if you could let me know in the comments. : bow:
Thank you for reading. : bow:
Recommended Posts