Extends the module object with module and instance accessors for class attributes, just like the native attr* accessors for instance attributes.

module AppConfiguration
  mattr_accessor :google_api_key
  self.google_api_key = "123456789"
  mattr_accessor :paypal_url
  self.paypal_url = "www.sandbox.paypal.com"
end
AppConfiguration.google_api_key = "overriding the api key!"

Also, modules included into Object need to be scanned and have their instance methods removed from blank slate. In theory, modules included into Kernel would have to be removed as well, but a “feature” of Ruby prevents late includes into modules from being exposed in the first place.

Methods
A
B
D
I
M
R
S
Included Modules
Instance Public methods
append_features(mod)
# File activesupport/lib/active_support/vendor/builder-2.1.2/blankslate.rb, line 105
def append_features(mod)
  result = blankslate_original_append_features(mod)
  return result if mod != Object
  instance_methods.each do |name|
    BlankSlate.hide(name)
  end
  result
end
as_load_path()

Returns String#underscore applied to the module name minus trailing classes.

ActiveRecord.as_load_path               # => "active_record"
ActiveRecord::Associations.as_load_path # => "active_record/associations"
ActiveRecord::Base.as_load_path         # => "active_record" (Base is a class)

The Kernel module gives an empty string by definition.

Kernel.as_load_path # => ""
Math.as_load_path   # => "math"
# File activesupport/lib/active_support/core_ext/module/loading.rb, line 12
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_accessor_with_default(sym, default = nil, &block)

Declare an attribute accessor with an initial default return value.

To give attribute :agethe initial value 25:

class Person
  attr_accessor_with_default :age, 25
end
some_person.age
=> 25
some_person.age = 26
some_person.age
=> 26

To give attribute :element_namea dynamic default value, evaluated in scope of self:

attr_accessor_with_default(:element_name) { name.underscore }
# File activesupport/lib/active_support/core_ext/module/attr_accessor_with_default.rb, line 21
  def attr_accessor_with_default(sym, default = nil, &block)
    raise 'Default value or block required' unless !default.nil? || block
    define_method(sym, block_given? ? block : Proc.new { default })
    module_eval("      def #{sym}=(value)                        # def age=(value)
        class << self; attr_reader :#{sym} end  #   class << self; attr_reader :age end
        @#{sym} = value                         #   @age = value
      end                                       # end
", __FILE__, __LINE__ + 1)
  end
attr_internal(*attrs)
attr_internal_accessor(*attrs)

Declares an attribute reader and writer backed by an internally-named instance variable.

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

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

# File 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)

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

# File 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
blankslate_original_append_features(mod)
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 :tooption 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
  belongs_to :greeter
  delegate :hello, :goodbye, :to => :greeter
end
Foo.new.goodbye # => "goodbye"

Methods can be delegated to instance variables, class variables, or constants by providing them as a symbols:

class Foo
  CONSTANT_ARRAY = [0,1,2,3]
  @@class_array  = [4,5,6,7]
  def initialize
    @instance_array = [8,9,10,11]
  end
  delegate :sum, :to => :CONSTANT_ARRAY
  delegate :min, :to => :@@class_array
  delegate :max, :to => :@instance_array
end
Foo.new.sum # => 6
Foo.new.min # => 4
Foo.new.max # => 11

Delegates can optionally be prefixed using the :prefixoption. If the value is true, the delegate methods are prefixed with the name of the object being delegated to.

Person = Struct.new(:name, :address)
class Invoice < Struct.new(:client)
  delegate :name, :address, :to => :client, :prefix => true
end
john_doe = Person.new("John Doe", "Vimmersvej 13")
invoice = Invoice.new(john_doe)
invoice.client_name    # => "John Doe"
invoice.client_address # => "Vimmersvej 13"

It is also possible to supply a custom prefix.

class Invoice < Struct.new(:client)
  delegate :name, :address, :to => :client, :prefix => :customer
end
invoice = Invoice.new(john_doe)
invoice.customer_name    # => "John Doe"
invoice.customer_address # => "Vimmersvej 13"

If the object to which you delegate can be nil, you may want to use the :allow_nil option. In that case, it returns nil instead of raising a NoMethodError exception:

class Foo
  attr_accessor :bar
  def initialize(bar = nil)
    @bar = bar
  end
  delegate :zoo, :to => :bar
end
Foo.new.zoo   # raises NoMethodError exception (you called nil.zoo)
class Foo
  attr_accessor :bar
  def initialize(bar = nil)
    @bar = bar
  end
  delegate :zoo, :to => :bar, :allow_nil => true
end
Foo.new.zoo   # returns nil
# File activesupport/lib/active_support/core_ext/module/delegation.rb, line 99
  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
    if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/
      raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
    end
    prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_"
    file, line = caller.first.split(':', 2)
    line = line.to_i
    methods.each do |method|
      on_nil =
        if options[:allow_nil]
          'return'
        else
          %Q(raise "#{prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
        end
      module_eval("        def #{prefix}#{method}(*args, &block)               # def customer_name(*args, &block)
          #{to}.__send__(#{method.inspect}, *args, &block)  #   client.__send__(:name, *args, &block)
        rescue NoMethodError                                # rescue NoMethodError
          if #{to}.nil?                                     #   if client.nil?
            #{on_nil}
          else                                              #   else
            raise                                           #     raise
          end                                               #   end
        end                                                 # end
", file, line)
    end
  end
included_in_classes()

Returns the classes in the current ObjectSpace where this module has been mixed in according to Module#included_modules.

module M
end
module N
  include M
end
class C
  include M
end
class D < C
end
p M.included_in_classes # => [C, D]
# File activesupport/lib/active_support/core_ext/module/inclusion.rb, line 21
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
mattr_accessor(*syms)
# File activesupport/lib/active_support/core_ext/module/attribute_accessors.rb, line 63
def mattr_accessor(*syms)
  mattr_reader(*syms)
  mattr_writer(*syms)
end
mattr_reader(*syms)
# File activesupport/lib/active_support/core_ext/module/attribute_accessors.rb, line 16
  def mattr_reader(*syms)
    options = syms.extract_options!
    syms.each do |sym|
      next if sym.is_a?(Hash)
      class_eval("        unless defined? @@#{sym}
          @@#{sym} = nil
        end
        def self.#{sym}
          @@#{sym}
        end
", __FILE__, __LINE__ + 1)
      unless options[:instance_reader] == false
        class_eval("          def #{sym}
            @@#{sym}
          end
", __FILE__, __LINE__ + 1)
      end
    end
  end
mattr_writer(*syms)
# File activesupport/lib/active_support/core_ext/module/attribute_accessors.rb, line 40
  def mattr_writer(*syms)
    options = syms.extract_options!
    syms.each do |sym|
      class_eval("        unless defined? @@#{sym}
          @@#{sym} = nil
        end
        def self.#{sym}=(obj)
          @@#{sym} = obj
        end
", __FILE__, __LINE__ + 1)
      unless options[:instance_writer] == false
        class_eval("          def #{sym}=(obj)
            @@#{sym} = obj
          end
", __FILE__, __LINE__ + 1)
      end
    end
  end
remove_possible_method(method)
# File activesupport/lib/active_support/core_ext/module/remove_method.rb, line 2
def remove_possible_method(method)
  remove_method(method)
rescue NameError
end
synchronize(*methods)

Synchronize access around a method, delegating synchronization to a particular mutex. A mutex (either a Mutex, or any object that responds to synchronize and yields to a block) must be provided as a final :with option. The :with option should be a symbol or string, and can represent a method, constant, or instance or class variable. Example:

class SharedCache
  @@lock = Mutex.new
  def expire
    ...
  end
  synchronize :expire, :with => :@@lock
end
# File activesupport/lib/active_support/core_ext/module/synchronization.rb, line 15
  def synchronize(*methods)
    options = methods.extract_options!
    unless options.is_a?(Hash) && with = options[:with]
      raise ArgumentError, "Synchronization needs a mutex. Supply an options hash with a :with key as the last argument (e.g. synchronize :hello, :with => :@mutex)."
    end
    methods.each do |method|
      aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
      if method_defined?("#{aliased_method}_without_synchronization#{punctuation}")
        raise ArgumentError, "#{method} is already synchronized. Double synchronization is not currently supported."
      end
      module_eval("        def #{aliased_method}_with_synchronization#{punctuation}(*args, &block)     # def expire_with_synchronization(*args, &block)
          #{with}.synchronize do                                                    #   @@lock.synchronize do
            #{aliased_method}_without_synchronization#{punctuation}(*args, &block)  #     expire_without_synchronization(*args, &block)
          end                                                                       #   end
        end                                                                         # end
", __FILE__, __LINE__ + 1)
      alias_method_chain method, :synchronization
    end
  end