# Why Module/Class/Object are redefined in Active Support?

**URL:** <https://www.ruby-forum.com/t/why-module-class-object-are-redefined-in-active-support/83750>\
**Category:** Rails\
**Created:** [January 29, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/why-module-class-object-are-redefined-in-active-support/83750 "2007-01-29T14:25:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 29, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/why-module-class-object-are-redefined-in-active-support/83750/1 "2007-01-29T14:25:49Z")

</div>

Recently i have decided to read sources of rails.In the source tree of  
Active Support, I found in the dependencies.rb that Module/Class/Object  
are redefined.

Can these keywords in ruby be redefined? And what are the main purposes  
to do that?And what’s more,since Module/Class/Object are redefined,then  
all the keywords emerged in the sources of activecontroller activeview  
activerecord use the definition in dependencies.rb,is it right?

class Module #:nodoc:

# Rename the original handler so we can chain it to the new one

alias :rails\_original\_const\_missing :const\_missing

# Use const\_missing to autoload associations so we don’t have to

# require\_association when using single-table inheritance.

def const\_missing(class\_id)  
Dependencies.load\_missing\_constant self, class\_id  
end

def unloadable(const\_desc = self)  
super(const\_desc)  
end

end

class Class  
def const\_missing(class\_id)  
if [Object, Kernel].include?(self) || parent == self  
super  
else  
begin  
begin  
Dependencies.load\_missing\_constant self, class\_id  
rescue NameError  
parent.send :const\_missing, class\_id  
end  
rescue NameError =\> e  
# Make sure that the name we are missing is the one that caused  
the error  
parent\_qualified\_name = Dependencies.qualified\_name\_for parent,  
class\_id  
raise unless e.missing\_name? parent\_qualified\_name  
qualified\_name = Dependencies.qualified\_name\_for self, class\_id  
raise NameError.new(“uninitialized constant  
#{qualified\_name}”).copy\_blame!(e)  
end  
end  
end  
end

class Object #:nodoc:

alias\_method :load\_without\_new\_constant\_marking, :load

def load(file, \*extras)  
Dependencies.new\_constants\_in(Object) { super(file, \*extras) }  
rescue Exception =\> exception # errors from loading file  
exception.blame\_file! file  
raise  
end

def require(file, \*extras)  
Dependencies.new\_constants\_in(Object) { super(file, \*extras) }  
rescue Exception =\> exception # errors from required file  
exception.blame\_file! file  
raise  
end

# Mark the given constant as unloadable. Unloadable constants are

removed each

# time dependencies are cleared.

# 

# Note that marking a constant for unloading need only be done once.

Setup

# or init scripts may list each unloadable constant that may need

unloading;

# each constant will be removed for every subsequent clear, as opposed

to for

# the first clear.

# 

# The provided constant descriptor may be a (non-anonymous) module or

class,

# or a qualified constant name as a string or symbol.

# 

# Returns true if the constant was not previously marked for

unloading, false

# otherwise.

def unloadable(const\_desc)  
Dependencies.mark\_for\_unload const\_desc  
end

end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 29, 2007, 3:00pm UTC](https://www.ruby-forum.com/t/why-module-class-object-are-redefined-in-active-support/83750/2 "2007-01-29T15:00:50Z")

</div>

Thanks,it is because of my chaotic concept of ruby lang.

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 29, 2007, 2:47pm UTC](https://www.ruby-forum.com/t/why-module-class-object-are-redefined-in-active-support/83750/3 "2007-01-29T14:47:18Z")

</div>

> Can these keywords in ruby be redefined? And what are the main purposes  
> to do that?  
> They’re not redefined. In ruby, a class/module is never ‘closed’ -  
> i.e. you can just add new methods and override old ones. Some people  
> call this ‘monkey patching’ and act like it’s a bad thing. To  
> rubyists, it’s a powerful feature. Rails extends and modifies the  
> core of ruby to add the features that we know and love. For example,  
> if I wanted to add a method to all ActiveRecord objects for my  
> application, I could just open up ActiveRecord and define it:

class ActiveRecord:Base  
def self.number\_of\_attributes  
@attributes.size  
end  
end

I could then use this method on any of my models:  
Person.number\_of\_attributes -\> 3

Hope that helps,

Steve
