Requiring a Ruby module in Rails console & controller

依然范特西╮ 提交于 2019-12-11 03:07:34

问题


I need the Ruby module FileUtils to handle some operations with files.

I want to test that this require will work correctly in my controller. So I tried this in my console but got false as a result (this works fine when I'm not in a Rails console but just a plain Ruby console):

  1. Go to the root directory of my project
  2. $ rails c to open my console
  3. > require 'fileutils' returns false.

What is the correct way to require a module via rails console? Why would this return false?

I'm assuming in my controller I can just do something like this, correct?

def create
  require 'fileutils'
  # my code that uses fileutils.
end

回答1:


A false response from require means that the module is already loaded. You should be good to go without requiring this in your controller. See docs: http://www.ruby-doc.org/core-2.1.1/Kernel.html#method-i-require




回答2:


It is most likely already required by default. Just type FileUtils in the console, it should respond

 # => FileUtils



回答3:


Returning false from require means that it's already loaded.

Try this from the console:

FileUtils.pwd
# => "/present/working/directory


来源:https://stackoverflow.com/questions/22022569/requiring-a-ruby-module-in-rails-console-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!