Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requiring a Ruby module in Rails console & controller

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
like image 368
Don P Avatar asked Dec 10 '25 14:12

Don P


2 Answers

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

like image 91
steakchaser Avatar answered Dec 13 '25 03:12

steakchaser


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

 # => FileUtils
like image 28
Daniël Knippers Avatar answered Dec 13 '25 02:12

Daniël Knippers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!