Undefined method error because I am not understanding Rails initializers

Hey all,

In app/builders, I have a file called table_builder.rb. In the
constructor method of my TableBuilder class, I call an instance method
called assign_attributes which takes a hash and converts the key/value
pairs into instance methods.

The assign_attributes method is declared in the following directory:
lib/core_extensions

In that directory I have a file called core.rb which contains the class
Object and the instance method assign_attributes is a method of Object
class (and so when you declare a class, since a class is an object, all
classes will inherit from the Object class since Object is the root
class of all classes):

class Object
def assign_attributes(attributes)
attributes.each_pair { |k,v| send("#{k}=", v) if
respond_to?("#{k}=") } if attributes
end
end

Hence, this instance method should be available to all classes,
including my TableBuilder class since TableBuilder implicitly inherits
from Object. The problem is I believe the Object class is never found.

In order to make it be found, in my initializers directory, I create a
file called ext.rb and add the following line:

require “core_extensions/core”
Dir[File.join(Rails.root, “lib”, “core_extensions”, “*.rb”)].each {|l|
require l }

This should require that core.rb script in all of the other scripts,
including builders/table_builder.rb

But when I load the page, I am greeted with an undefined method
exception:

undefined method `assign_attributes’ for
#TableBuilder:0x00000100fc7708

Thanks for response.

I restarted the server and it appears to have worked.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs