Class: Object

Inherits:
BasicObject
Includes:
InstanceExecMethods
Defined in:
lib/active_support/vendor/builder-2.1.2/blankslate.rb,
lib/active_support/dependencies.rb,
lib/active_support/core_ext/blank.rb,
lib/active_support/core_ext/duplicable.rb,
lib/active_support/core_ext/object/misc.rb,
lib/active_support/json/encoders/object.rb,
lib/active_support/core_ext/kernel/agnostics.rb,
lib/active_support/core_ext/object/extending.rb,
lib/active_support/core_ext/object/conversions.rb,
lib/active_support/core_ext/object/instance_variables.rb

Overview

Same as above, except in Object.

Defined Under Namespace

Modules: InstanceExecMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.blank_slate_method_addedObject


79
# File 'lib/active_support/vendor/builder-2.1.2/blankslate.rb', line 79

alias_method :blank_slate_method_added, :method_added

.find_hidden_method(name) ⇒ Object


90
91
92
# File 'lib/active_support/vendor/builder-2.1.2/blankslate.rb', line 90

def find_hidden_method(name)
  nil
end

.method_added(name) ⇒ Object

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


83
84
85
86
87
88
# File 'lib/active_support/vendor/builder-2.1.2/blankslate.rb', line 83

def method_added(name)
  result = blank_slate_method_added(name)
  return result if self != Object
  BlankSlate.hide(name)
  result
end

Instance Method Details

#`(command) ⇒ Object

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.


6
7
8
9
10
# File 'lib/active_support/core_ext/kernel/agnostics.rb', line 6

def `(command) #:nodoc:
  super
rescue Errno::ENOENT => e
  STDERR.puts "#$0: #{e}"
end

#acts_like?(duck) ⇒ Boolean

A duck-type assistant method. For example, Active Support 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.

Returns:

  • (Boolean)

56
57
58
# File 'lib/active_support/core_ext/object/misc.rb', line 56

def acts_like?(duck)
  respond_to? "acts_like_#{duck}?"
end

#blank?Boolean

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?

Returns:

  • (Boolean)

12
13
14
# File 'lib/active_support/core_ext/blank.rb', line 12

def blank?
  respond_to?(:empty?) ? empty? : !self
end

#copy_instance_variables_from(object, exclude = []) ⇒ Object

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

68
69
70
71
72
73
# File 'lib/active_support/core_ext/object/instance_variables.rb', line 68

def copy_instance_variables_from(object, exclude = []) #:nodoc:
  exclude += object.protected_instance_variables if object.respond_to? :protected_instance_variables

  vars = object.instance_variables.map(&:to_s) - exclude.map(&:to_s)
  vars.each { |name| instance_variable_set(name, object.instance_variable_get(name)) }
end

#duplicable?Boolean

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

Returns:

  • (Boolean)

4
5
6
# File 'lib/active_support/core_ext/duplicable.rb', line 4

def duplicable?
  true
end

#extend_with_included_modules_from(object) ⇒ Object

:nodoc:


28
29
30
# File 'lib/active_support/core_ext/object/extending.rb', line 28

def extend_with_included_modules_from(object) #:nodoc:
  object.extended_by.each { |mod| extend mod }
end

#extended_byObject

:nodoc:


23
24
25
26
# File 'lib/active_support/core_ext/object/extending.rb', line 23

def extended_by #:nodoc:
  ancestors = class << self; ancestors end
  ancestors.select { |mod| mod.class == Module } - [ Object, Kernel ]
end

#instance_exec(*args, &block) ⇒ Object

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


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/active_support/core_ext/object/extending.rb', line 41

def instance_exec(*args, &block)
  begin
    old_critical, Thread.critical = Thread.critical, true
    n = 0
    n += 1 while respond_to?(method_name = "__instance_exec#{n}")
    InstanceExecMethods.module_eval { define_method(method_name, &block) }
  ensure
    Thread.critical = old_critical
  end

  begin
    send(method_name, *args)
  ensure
    InstanceExecMethods.module_eval { remove_method(method_name) } rescue nil
  end
end

#instance_valuesObject

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}

19
20
21
22
23
24
# File 'lib/active_support/core_ext/object/instance_variables.rb', line 19

def instance_values #:nodoc:
  instance_variables.inject({}) do |values, name|
    values[name.to_s[1..-1]] = instance_variable_get(name)
    values
  end
end

#instance_variable_defined?(variable) ⇒ Boolean

Returns:

  • (Boolean)

4
5
6
# File 'lib/active_support/core_ext/object/instance_variables.rb', line 4

def instance_variable_defined?(variable)
  instance_variables.include?(variable.to_s)
end

#load(file, *extras) ⇒ Object

:nodoc:


501
502
503
504
505
506
# File 'lib/active_support/dependencies.rb', line 501

def load(file, *extras) #:nodoc:
  Dependencies.new_constants_in(Object) { super }
rescue Exception => exception  # errors from loading file
  exception.blame_file! file
  raise
end

#load_without_new_constant_markingObject


499
# File 'lib/active_support/dependencies.rb', line 499

alias_method :load_without_new_constant_marking, :load

#remove_subclasses_of(*superclasses) ⇒ Object

:nodoc:


2
3
4
# File 'lib/active_support/core_ext/object/extending.rb', line 2

def remove_subclasses_of(*superclasses) #:nodoc:
  Class.remove_class(*subclasses_of(*superclasses))
end

#require(file, *extras) ⇒ Object

:nodoc:


508
509
510
511
512
513
# File 'lib/active_support/dependencies.rb', line 508

def require(file, *extras) #:nodoc:
  Dependencies.new_constants_in(Object) { super }
rescue Exception => exception  # errors from required file
  exception.blame_file! file
  raise
end

#returning(value) {|value| ... } ⇒ Object

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']

Yields:

  • (value)

27
28
29
30
# File 'lib/active_support/core_ext/object/misc.rb', line 27

def returning(value)
  yield(value)
  value
end

#subclasses_of(*superclasses) ⇒ Object

:nodoc:


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/active_support/core_ext/object/extending.rb', line 6

def subclasses_of(*superclasses) #:nodoc:
  subclasses = []

  # Exclude this class unless it's a subclass of our supers and is defined.
  # We check defined? in case we find a removed class that has yet to be
  # garbage collected. This also fails for anonymous classes -- please
  # submit a patch if you have a workaround.
  ObjectSpace.each_object(Class) do |k|
    if superclasses.any? { |superclass| k < superclass } &&
      (k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id"))
      subclasses << k
    end
  end

  subclasses
end

#to_json(options = {}) ⇒ Object

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


3
4
5
# File 'lib/active_support/json/encoders/object.rb', line 3

def to_json(options = {})
  ActiveSupport::JSON.encode(instance_values, options)
end

#to_paramObject

Alias of to_s.


3
4
5
# File 'lib/active_support/core_ext/object/conversions.rb', line 3

def to_param
  to_s
end

#to_query(key) ⇒ Object

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.


11
12
13
# File 'lib/active_support/core_ext/object/conversions.rb', line 11

def to_query(key)
  "#{CGI.escape(key.to_s)}=#{CGI.escape(to_param.to_s)}"
end

#unloadable(const_desc) ⇒ Object

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.


528
529
530
# File 'lib/active_support/dependencies.rb', line 528

def unloadable(const_desc)
  Dependencies.mark_for_unload const_desc
end

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

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

Yields:


47
48
49
# File 'lib/active_support/core_ext/object/misc.rb', line 47

def with_options(options)
  yield ActiveSupport::OptionMerger.new(self, options)
end