Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby mixin gives unidentified constant error

Tags:

ruby

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?

like image 963
Bilal and Olga Avatar asked Mar 14 '10 00:03

Bilal and Olga


2 Answers

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.

like image 73
Gareth Avatar answered Sep 19 '22 20:09

Gareth


Did you try?

class Text
  include ::FileUtils
end

This assumes that FileUtils is not within a module.

like image 34
Randy Simon Avatar answered Sep 18 '22 20:09

Randy Simon