Class: Object

Inherits:
BasicObject
Defined in:
activesupport/lib/active_support/vendor/builder-2.1.2/blankslate.rb,
activesupport/lib/active_support/core_ext/try.rb,
actionpack/lib/action_controller/routing/routing_ext.rb,
activesupport/lib/active_support/core_ext/duplicable.rb,
activesupport/lib/active_support/core_ext/object/misc.rb,
activesupport/lib/active_support/json/encoders/object.rb,
activesupport/lib/active_support/core_ext/object/blank.rb,
activesupport/lib/active_support/core_ext/object/extending.rb,
activesupport/lib/active_support/core_ext/object/metaclass.rb,
activesupport/lib/active_support/core_ext/kernel/agnostics.rb,
activesupport/lib/active_support/core_ext/object/conversions.rb,
activesupport/lib/active_support/core_ext/string/output_safety.rb,
activesupport/lib/active_support/core_ext/object/singleton_class.rb,
activesupport/lib/active_support/core_ext/object/instance_variables.rb,
actionmailer/lib/action_mailer/vendor/tmail-1.2.7/tmail/core_extensions.rb

Overview

Same as above, except in Object.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) blank_slate_method_added



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

alias_method :blank_slate_method_added, :method_added

+ (Object) find_hidden_method(name)



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

def find_hidden_method(name)
  nil
end

+ (Object) method_added(name)

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



83
84
85
86
87
88
# File 'activesupport/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

- (Object) `(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.



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

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

- (Boolean) acts_like?(duck)

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.



86
87
88
# File 'activesupport/lib/active_support/core_ext/object/misc.rb', line 86

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

- (Object) as_json(options = nil)



7
8
9
# File 'activesupport/lib/active_support/json/encoders/object.rb', line 7

def as_json(options = nil)
  instance_values
end

- (Boolean) blank?

An object is blank if it's nil, empty, or a whitespace string. For example, “”, “ ”, nil, [], and {} are blank.

This simplifies

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

to

if !address.blank?


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

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

- (Object) class_eval(*args, &block)

class_eval on an object acts like singleton_class_eval.



10
11
12
# File 'activesupport/lib/active_support/core_ext/object/singleton_class.rb', line 10

def class_eval(*args, &block)
  singleton_class.class_eval(*args, &block)
end

- (Object) 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


68
69
70
71
72
73
# File 'activesupport/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

- (Boolean) duplicable?

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



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

def duplicable?
  true
end

- (Object) extend_with_included_modules_from(object)

:nodoc:



50
51
52
# File 'activesupport/lib/active_support/core_ext/object/extending.rb', line 50

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

- (Object) extended_by

:nodoc:



45
46
47
48
# File 'activesupport/lib/active_support/core_ext/object/extending.rb', line 45

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

- (Boolean) html_safe?



52
53
54
# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 52

def html_safe?
  false
end

- (Object) 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}


19
20
21
22
23
24
# File 'activesupport/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

- (Boolean) instance_variable_defined?(variable)



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

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

- (Object) metaclass

Get object's meta (ghost, eigenclass, singleton) class.

Deprecated in favor of Object#singleton_class.



7
8
9
10
11
# File 'activesupport/lib/active_support/core_ext/object/metaclass.rb', line 7

def metaclass
  class << self
    self
  end
end

- (Object) presence

Returns object if it's #present? otherwise returns nil. object.presence is equivalent to object.present? ? object : nil.

This is handy for any representation of objects where blank is the same as not present at all. For example, this simplifies a common check for HTTP POST/query parameters:

state   = params[:state]   if params[:state].present?
country = params[:country] if params[:country].present?
region  = state || country || 'US'

…becomes:

region = params[:state].presence || params[:country].presence || 'US'


35
36
37
# File 'activesupport/lib/active_support/core_ext/object/blank.rb', line 35

def presence
  self if present?
end

- (Boolean) present?

An object is present if it's not blank.



17
18
19
# File 'activesupport/lib/active_support/core_ext/object/blank.rb', line 17

def present?
  !blank?
end

- (Object) remove_subclasses_of(*superclasses)

:nodoc:



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

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

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

Returns value after yielding value to the block. This simplifies the process of constructing an object, performing work on the object, and then returning the object from a method. It is a Ruby-ized realization of the K combinator, courtesy of Mikael Brockman.

Examples

# Without returning
def foo
  values = []
  values << "bar"
  values << "baz"
  return values
end

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

# returning with a local variable
def foo
  returning values = [] do
    values << 'bar'
    values << 'baz'
  end
end

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

# returning with a block argument
def foo
  returning [] do |values|
    values << 'bar'
    values << 'baz'
  end
end

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

Yields:

  • (value)


38
39
40
41
# File 'activesupport/lib/active_support/core_ext/object/misc.rb', line 38

def returning(value)
  yield(value)
  value
end

- (Object) singleton_class



3
4
5
6
7
# File 'activesupport/lib/active_support/core_ext/object/singleton_class.rb', line 3

def singleton_class
  class << self
    self
  end
end

- (Object) tap {|_self| ... }

Yields:

  • (_self)

Yield Parameters:

  • _self (Object)

    the object that the method was called on



53
54
55
56
# File 'activesupport/lib/active_support/core_ext/object/misc.rb', line 53

def tap
  yield self
  self
end

- (Object) to_json(options = nil)

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



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

def to_json(options = nil)
  ActiveSupport::JSON.encode(as_json(options))
end

- (Object) to_param

Alias of to_s.



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

def to_param
  to_s
end

- (Object) 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.



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

def to_query(key)
  require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
  "#{CGI.escape(key.to_s)}=#{CGI.escape(to_param.to_s)}"
end

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

An elegant way to factor duplication out of options passed to a series of method calls. Each method called in the block, with the block variable as the receiver, will have its options merged with the default options hash provided. Each method called on the block variable must take an options hash as its final argument.

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:



77
78
79
# File 'activesupport/lib/active_support/core_ext/object/misc.rb', line 77

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