tags:

views:

128

answers:

2

In irb, I do this

class Text
  include FileUtils
end

I get: NameError: uninitialized constant Test::FileUtils

If I just do: include FileUtils (i.e. now class) everthing works.

What gives?

+1  A: 

Did you try?

class Text
  include ::FileUtils
end

This assumes that FileUtils is not within a module.

Randy Simon
Doesn't work ...irb(main):004:0> class Testirb(main):005:1> include ::FileUtilsirb(main):006:1> endNameError: uninitialized constant FileUtils from (irb):5
Bilal Aslam
+2  A: 

You need to make sure Ruby knows about the FileUtils module. That module isn't loaded by default:

>> FileUtils
NameError: uninitialized constant FileUtils
    from (irb):1
>> require 'fileutils'
=> true
>> FileUtils
=> FileUtils

Don't worry too much about the error NameError: uninitialized constant Text::FileUtils - when you try to include a constant that Ruby doesn't know about, it looks in a few places. In your case, first it will look for Text::FileUtils and then it will look for ::FileUtils in the root namespace. If it can't find it anywhere (which in your case it couldn't) then the error message will tell you the first place it looked.

Gareth