Methods
A
D
I
L
P
Instance Public methods
alias_attribute(new_name, old_name)

Allows you to make aliases for attributes, which includes getter, setter, and query methods.

Example:

class Content < ActiveRecord::Base
  # has a title attribute
end

class Email < ActiveRecord::Base
  alias_attribute :subject, :title
end

e = Email.find(1)
e.title    # => "Superstars"
e.subject  # => "Superstars"
e.subject? # => true
e.subject = "Megastars"
e.title    # => "Megastars"
# File rails/activesupport/lib/active_support/core_ext/module/aliasing.rb, line 51
  def alias_attribute(new_name, old_name)
    module_eval "      def #{new_name}; #{old_name}; end
      def #{new_name}?; #{old_name}?; end
      def #{new_name}=(v); self.#{old_name} = v; end
", __FILE__, __LINE__+1
  end
alias_method_chain(target, feature)

Encapsulates the common pattern of:

alias_method :foo_without_feature, :foo
alias_method :foo, :foo_with_feature

With this, you simply do:

alias_method_chain :foo, :feature

And both aliases are set up for you.

Query and bang methods (foo?, foo!) keep the same punctuation:

alias_method_chain :foo?, :feature

is equivalent to

alias_method :foo_without_feature?, :foo?
alias_method :foo?, :foo_with_feature?

so you can safely chain foo, foo?, and foo! with the same feature.

# File rails/activesupport/lib/active_support/core_ext/module/aliasing.rb, line 23
def alias_method_chain(target, feature)
  # Strip out punctuation on predicates or bang methods since
  # e.g. target?_without_feature is not a valid method name.
  aliased_target, punctuation = target.to_s.sub(%r([?!=])$/, ''), $1
  yield(aliased_target, punctuation) if block_given?
  alias_method "#{aliased_target}_without_#{feature}#{punctuation}", target
  alias_method target, "#{aliased_target}_with_#{feature}#{punctuation}"
end
as_load_path()
# File rails/activesupport/lib/active_support/core_ext/module/loading.rb, line 2
def as_load_path
  if self == Object || self == Kernel
    ''
  elsif is_a? Class
    parent == self ? '' : parent.as_load_path
  else
    name.split('::').collect do |word|
      word.underscore
    end * '/'
  end
end
attr_internal(*attrs)
attr_internal_accessor(*attrs)

Declare attributes backed by ‘internal’ instance variables names.

Also aliased as: attr_internal
# File rails/activesupport/lib/active_support/core_ext/module/attr_internal.rb, line 17
def attr_internal_accessor(*attrs)
  attr_internal_reader(*attrs)
  attr_internal_writer(*attrs)
end
attr_internal_reader(*attrs)

Declare an attribute reader backed by an internally-named instance variable.

# File rails/activesupport/lib/active_support/core_ext/module/attr_internal.rb, line 3
def attr_internal_reader(*attrs)
  attrs.each do |attr|
    module_eval "def #{attr}() #{attr_internal_ivar_name(attr)} end"
  end
end
attr_internal_writer(*attrs)

Declare an attribute writer backed by an internally-named instance variable.

# File rails/activesupport/lib/active_support/core_ext/module/attr_internal.rb, line 10
def attr_internal_writer(*attrs)
  attrs.each do |attr|
    module_eval "def #{attr}=(v) #{attr_internal_ivar_name(attr)} = v end"
  end
end
delegate(*methods)

Provides a delegate class method to easily expose contained objects’ methods as your own. Pass one or more methods (specified as symbols or strings) and the name of the target object as the final :to option (also a symbol or string). At least one method and the :to option are required.

Delegation is particularly useful with Active Record associations:

class Greeter < ActiveRecord::Base
  def hello()   "hello"   end
  def goodbye() "goodbye" end
end

class Foo < ActiveRecord::Base
  belongs_to :greeter
  delegate :hello, :to => :greeter
end

Foo.new.hello   # => "hello"
Foo.new.goodbye # => NoMethodError: undefined method `goodbye' for #<Foo:0x1af30c>

Multiple delegates to the same target are allowed:

class Foo < ActiveRecord::Base
  delegate :hello, :goodbye, :to => :greeter
end

Foo.new.goodbye # => "goodbye"
# File rails/activesupport/lib/active_support/core_ext/module/delegation.rb, line 27
  def delegate(*methods)
    options = methods.pop
    unless options.is_a?(Hash) && to = options[:to]
      raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
    end

    methods.each do |method|
      module_eval("        def #{method}(*args, &block)
          #{to}.__send__(#{method.inspect}, *args, &block)
        end
", "(__DELEGATION__)", 1)
    end
  end
include_all_modules_from(parent_module)
# File rails/railties/lib/console_with_helpers.rb, line 2
def include_all_modules_from(parent_module)
  parent_module.constants.each do |const|
    mod = parent_module.const_get(const)
    if mod.class == Module
      send(:include, mod)
      include_all_modules_from(mod)
    end
  end
end
included_in_classes()
# File rails/activesupport/lib/active_support/core_ext/module/inclusion.rb, line 2
def included_in_classes
  classes = []
  ObjectSpace.each_object(Class) { |k| classes << k if k.included_modules.include?(self) }

  classes.reverse.inject([]) do |unique_classes, klass| 
    unique_classes << klass unless unique_classes.collect { |k| k.to_s }.include?(klass.to_s)
    unique_classes
  end
end
local_constants()

Return the constants that have been defined locally by this object and not in an ancestor. This method may miss some constants if their definition in the ancestor is identical to their definition in the receiver.

# File rails/activesupport/lib/active_support/core_ext/module/introspection.rb, line 25
def local_constants
  inherited = {}
  ancestors.each do |anc|
    next if anc == self
    anc.constants.each { |const| inherited[const] = anc.const_get(const) }
  end
  constants.select do |const|
    ! inherited.key?(const) || inherited[const].object_id != const_get(const).object_id
  end
end
parent()

Return the module which contains this one; if this is a root module, such as +::MyModule+, then Object is returned.

# File rails/activesupport/lib/active_support/core_ext/module/introspection.rb, line 4
def parent
  parent_name = name.split('::')[0..-2] * '::'
  parent_name.empty? ? Object : parent_name.constantize
end
parents()

Return all the parents of this module, ordered from nested outwards. The receiver is not contained within the result.

# File rails/activesupport/lib/active_support/core_ext/module/introspection.rb, line 11
def parents
  parents = []
  parts = name.split('::')[0..-2]
  until parts.empty?
    parents << (parts * '::').constantize
    parts.pop
  end
  parents << Object unless parents.include? Object
  parents
end