Requiring a Ruby module in Rails console & control

2019-07-15 11:40发布

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

3条回答
聊天终结者
2楼-- · 2019-07-15 12:02

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

查看更多
Luminary・发光体
3楼-- · 2019-07-15 12:13

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

 # => FileUtils
查看更多
Viruses.
4楼-- · 2019-07-15 12:25

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

Try this from the console:

FileUtils.pwd
# => "/present/working/directory
查看更多
登录 后发表回答