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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Steve Bartholomew
2007-Jan-29 13:46 UTC
Re: Why Module/Class/Object are redefined in Active Support?
> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
fredzhang
2007-Jan-29 14:00 UTC
Re: Why Module/Class/Object are redefined in Active Support?
Thanks,it is because of my chaotic concept of ruby lang. Fred -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---