Class: Object

Same as above, except in Object.

Child modules and classes

Module Object::InstanceExecMethods

Aliases

Old nameNew name
load load_without_new_constant_marking
send send!
method_added blank_slate_method_added

Public Class Methods


find_hidden_method (name)

    # File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/blankslate.rb, line 90
90:     def find_hidden_method(name)
91:       nil
92:     end

lookup_missing_generator (class_id)

Lookup missing generators using const_missing. This allows any generator to reference another without having to know its location: RubyGems, ~/.rails/generators, and RAILS_ROOT/generators.

    # File vendor/rails/railties/lib/rails_generator/lookup.rb, line 10
10:     def lookup_missing_generator(class_id)
11:       if md = /(.+)Generator$/.match(class_id.to_s)
12:         name = md.captures.first.demodulize.underscore
13:         Rails::Generator::Base.lookup(name).klass
14:       else
15:         const_missing_before_generators(class_id)
16:       end
17:     end

method_added (name)

Detect method additions to Object and remove them in the BlankSlate class.

    # File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/blankslate.rb, line 83
83:     def method_added(name)
84:       result = blank_slate_method_added(name)
85:       return result if self != Object
86:       BlankSlate.hide(name)
87:       result
88:     end

Public Instance Methods


` (command)

Makes backticks behave (somewhat more) similarly on all platforms. On win32 `nonexistent_command` raises Errno::ENOENT; on Unix, the spawned shell prints a message to stderr and sets $?. We emulate Unix on the former but not the latter.

    # File vendor/rails/activesupport/lib/active_support/core_ext/kernel/agnostics.rb, line 6
 6:   def `(command) #nodoc
 7:     super
 8:   rescue Errno::ENOENT => e
 9:     STDERR.puts "#$0: #{e}"
10:   end

acts_like? (duck)

A duck-type assistant method. For example, ActiveSupport extends Date to define an acts_like_date? method, and extends Time to define acts_like_time?. As a result, we can do "x.acts_like?(:time)" and "x.acts_like?(:date)" to do duck-type-safe comparisons, since classes that we want to act like Time simply need to define an acts_like_time? method.

    # File vendor/rails/activesupport/lib/active_support/core_ext/object/misc.rb, line 56
56:   def acts_like?(duck)
57:     respond_to? "acts_like_#{duck}?"
58:   end

blank? ()

An object is blank if it‘s false, empty, or a whitespace string. For example, "", " ", nil, [], and {} are blank.

This simplifies

  if !address.nil? && !address.empty?

to

  if !address.blank?
    # File vendor/rails/activesupport/lib/active_support/core_ext/blank.rb, line 9
 9:   def blank?
10:     respond_to?(:empty?) ? empty? : !self
11:   end

copy_instance_variables_from (object, exclude = [])

Copies the instance variables of object into self.

Instance variable names in the exclude array are ignored. If object responds to protected_instance_variables the ones returned are also ignored. For example, Rails controllers implement that method.

In both cases strings and symbols are understood, and they have to include the at sign.

  class C
    def initialize(x, y, z)
      @x, @y, @z = x, y, z
    end

    def protected_instance_variables
      %w(@z)
    end
  end

  a = C.new(0, 1, 2)
  b = C.new(3, 4, 5)

  a.copy_instance_variables_from(b, [:@y])
  # a is now: @x = 3, @y = 1, @z = 2
    # File vendor/rails/activesupport/lib/active_support/core_ext/object/instance_variables.rb, line 64
64:   def copy_instance_variables_from(object, exclude = []) #nodoc
65:     exclude += object.protected_instance_variables if object.respond_to? :protected_instance_variables
66: 
67:     vars = object.instance_variables.map(&:to_s) - exclude.map(&:to_s)
68:     vars.each { |name| instance_variable_set(name, object.instance_variable_get(name)) }
69:   end

duplicable? ()

Can you safely .dup this object? False for nil, false, true, symbols, and numbers; true otherwise.

   # File vendor/rails/activesupport/lib/active_support/core_ext/duplicable.rb, line 4
4:   def duplicable?
5:     true
6:   end

extend_with_included_modules_from (object)

    # File vendor/rails/activesupport/lib/active_support/core_ext/object/extending.rb, line 28
28:   def extend_with_included_modules_from(object) #nodoc
29:     object.extended_by.each { |mod| extend mod }
30:   end

extended_by (

    # File vendor/rails/activesupport/lib/active_support/core_ext/object/extending.rb, line 23
23:   def extended_by #nodoc
24:     ancestors = class << self; ancestors end
25:     ancestors.select { |mod| mod.class == Module } - [ Object, Kernel ]
26:   end

instance_exec (*args, &block)

Evaluate the block with the given arguments within the context of this object, so self is set to the method receiver.

From Mauricio‘s eigenclass.org/hiki/bounded+space+instance_exec

    # File vendor/rails/activesupport/lib/active_support/core_ext/object/extending.rb, line 41
41:     def instance_exec(*args, &block)
42:       begin
43:         old_critical, Thread.critical = Thread.critical, true
44:         n = 0
45:         n += 1 while respond_to?(method_name = "__instance_exec#{n}")
46:         InstanceExecMethods.module_eval { define_method(method_name, &block) }
47:       ensure
48:         Thread.critical = old_critical
49:       end
50: 
51:       begin
52:         send(method_name, *args)
53:       ensure
54:         InstanceExecMethods.module_eval { remove_method(method_name) } rescue nil
55:       end
56:     end

instance_values (

Returns a hash that maps instance variable names without "@" to their corresponding values. Keys are strings both in Ruby 1.8 and 1.9.

  class C
    def initialize(x, y)
      @x, @y = x, y
    end
  end

  C.new(0, 1).instance_values # => {"x" => 0, "y" => 1}
    # File vendor/rails/activesupport/lib/active_support/core_ext/object/instance_variables.rb, line 19
19:   def instance_values #nodoc
20:     instance_variables.inject({}) do |values, name|
21:       values[name.to_s[1..-1]] = instance_variable_get(name)
22:       values
23:     end
24:   end

instance_variable_defined? (variable)

   # File vendor/rails/activesupport/lib/active_support/core_ext/object/instance_variables.rb, line 4
4:     def instance_variable_defined?(variable)
5:       instance_variables.include?(variable.to_s)
6:     end

instance_variable_names ()

Returns an array of instance variable names including "@". They are strings both in Ruby 1.8 and 1.9.

  class C
    def initialize(x, y)
      @x, @y = x, y
    end
  end

  C.new(0, 1).instance_variable_names # => ["@y", "@x"]
    # File vendor/rails/activesupport/lib/active_support/core_ext/object/instance_variables.rb, line 36
36:   def instance_variable_names
37:     instance_variables.map(&:to_s)
38:   end

load (file, *extras)

     # File vendor/rails/activesupport/lib/active_support/dependencies.rb, line 487
487:   def load(file, *extras) #nodoc
488:     Dependencies.new_constants_in(Object) { super }
489:   rescue Exception => exception  # errors from loading file
490:     exception.blame_file! file
491:     raise
492:   end

remove_subclasses_of (*superclasses)

   # File vendor/rails/activesupport/lib/active_support/core_ext/object/extending.rb, line 2
2:   def remove_subclasses_of(*superclasses) #nodoc
3:     Class.remove_class(*subclasses_of(*superclasses))
4:   end

require (file, *extras)

     # File vendor/rails/activesupport/lib/active_support/dependencies.rb, line 494
494:   def require(file, *extras) #nodoc
495:     Dependencies.new_constants_in(Object) { super }
496:   rescue Exception => exception  # errors from required file
497:     exception.blame_file! file
498:     raise
499:   end

returning (value) {|value| ...}

A Ruby-ized realization of the K combinator, courtesy of Mikael Brockman.

  def foo
    returning values = [] do
      values << 'bar'
      values << 'baz'
    end
  end

  foo # => ['bar', 'baz']

  def foo
    returning [] do |values|
      values << 'bar'
      values << 'baz'
    end
  end

  foo # => ['bar', 'baz']
    # File vendor/rails/activesupport/lib/active_support/core_ext/object/misc.rb, line 27
27:   def returning(value)
28:     yield(value)
29:     value
30:   end

subclasses_of (*superclasses)

    # File vendor/rails/activesupport/lib/active_support/core_ext/object/extending.rb, line 6
 6:   def subclasses_of(*superclasses) #nodoc
 7:     subclasses = []
 8: 
 9:     # Exclude this class unless it's a subclass of our supers and is defined.
10:     # We check defined? in case we find a removed class that has yet to be
11:     # garbage collected. This also fails for anonymous classes -- please
12:     # submit a patch if you have a workaround.
13:     ObjectSpace.each_object(Class) do |k|
14:       if superclasses.any? { |superclass| k < superclass } &&
15:         (k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id"))
16:         subclasses << k
17:       end
18:     end
19: 
20:     subclasses
21:   end

to_json (options = {})

Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.

   # File vendor/rails/activesupport/lib/active_support/json/encoders/object.rb, line 3
3:   def to_json(options = {})
4:     ActiveSupport::JSON.encode(instance_values, options)
5:   end

to_param ()

Alias of to_s.

   # File vendor/rails/activesupport/lib/active_support/core_ext/object/conversions.rb, line 3
3:   def to_param
4:     to_s
5:   end

to_param ()

   # File vendor/rails/actionpack/lib/action_controller/routing/routing_ext.rb, line 3
3:   def to_param
4:     to_s
5:   end

to_query (key)

Converts an object into a string suitable for use as a URL query string, using the given key as the param name.

Note: This method is defined as a default implementation for all Objects for Hash#to_query to work.

    # File vendor/rails/activesupport/lib/active_support/core_ext/object/conversions.rb, line 11
11:   def to_query(key)
12:     "#{CGI.escape(key.to_s)}=#{CGI.escape(to_param.to_s)}"
13:   end

unloadable (const_desc)

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.

     # File vendor/rails/activesupport/lib/active_support/dependencies.rb, line 514
514:   def unloadable(const_desc)
515:     Dependencies.mark_for_unload const_desc
516:   end

with_options (options) {|ActiveSupport::OptionMerger.new(self, options)| ...}

An elegant way to refactor out common options

  with_options :order => 'created_at', :class_name => 'Comment' do |post|
    post.has_many :comments, :conditions => ['approved = ?', true], :dependent => :delete_all
    post.has_many :unapproved_comments, :conditions => ['approved = ?', false]
    post.has_many :all_comments
  end

Can also be used with an explicit receiver:

  map.with_options :controller => "people" do |people|
    people.connect "/people",     :action => "index"
    people.connect "/people/:id", :action => "show"
  end
    # File vendor/rails/activesupport/lib/active_support/core_ext/object/misc.rb, line 47
47:   def with_options(options)
48:     yield ActiveSupport::OptionMerger.new(self, options)
49:   end