class Object

Ruby’s core Object class. See documentation for version 2.1.5, 2.0.0, or 1.9.3.

Ruby’s core Object class. See documentation for version 2.1.3, 2.0.0, or 1.9.3.

Public Instance Methods

blank?() click to toggle source

When called on a generic object, the #blank? method returns true if the object is false, empty, or nil. Other objects return false. For specific examples of different classes, see the class-specific definitions of #blank?.

Time.now.blank?  # => false
   # File lib/reactive_support/core_ext/object/blank.rb
18 def blank?
19   respond_to?(:empty) ? !!empty : !self
20 end
deep_dup() click to toggle source

The #deep_dup method returns a duplicate of a duplicable object. If the object calling #deep_dup is not duplicable, the object itself is returned. #deep_dup is overwritten in the Array and Hash classes (see ./object/deep_dup.rb). In those classes, it duplicates the object recursively so the members of the enumerable can be manipulated without affecting the original object.

   # File lib/reactive_support/core_ext/object/deep_dup.rb
16 def deep_dup
17   duplicable? ? self.dup : self
18 end
duplicable?() click to toggle source

The #duplicable? method checks whether an object may be safely duplicated. It returns true, unless the object calling it has its own method called #duplicable?. The #duplicable? method is defined for non-duplicable classes in ./object/duplicable.rb.

   # File lib/reactive_support/core_ext/object/duplicable.rb
 8 def duplicable?
 9   true
10 end
exist?()
Alias for: exists?
exists?() click to toggle source

The #exists? method and its alias, #exist?, return true if the object calling the method is not nil.

'foobar'.exists?      # => true
nil.exists?           # => false
   # File lib/reactive_support/core_ext/object/exist.rb
 9 def exists?
10   !self.nil?
11 end
Also aliased as: exist?
in?(object) click to toggle source

The #in? method returns true if the calling object is included in the object passed as a parameter. The object parameter must be a String, Enumerable, or other object that responds to #include?. If it doesn’t, the in? method will raise an ArgumentError.

When passed an Array object, #in? returns true if the calling object is included as a member of the array:

'foo'.in? ['foo', 'bar']    # => true
'foo'.in? ['bar', 'baz']    # => false

When passed a Hash object, #in? returns true if the calling object is included as a key in the hash. To look for an object in a hash’s values, use the hash’s #values method.

'foo'.in? {'foo' => 'bar'}          # => true
'foo'.in? {'bar' => 'foo'}          # => false
'foo'.in? {'bar' => 'foo'}.values   # => true

When passed a String object, #in? returns true if the the calling object (which must also be a string) appears verbatim within the parameter object. If the passed-in object is a string but the calling object is something else, a TypeError will be returned.

'foo'.in? 'foobar'    # => true
'foo'.in? 'foto'      # => false
['foo'].in? 'foobar'  # => TypeError

When passed an object that does not respond to #include?, #in? raises an ArgumentError.

   # File lib/reactive_support/core_ext/object/inclusion.rb
34 def in?(object)
35   object.include?(self)
36 rescue NoMethodError
37   raise ArgumentError.new("The parameter passed to #in? must respond to #include?")
38 end
instance_values() click to toggle source

The #instance_values method returns a hash mapping all the calling object’s instance variable names to the variables’ values. The hash keys are the variables’ names, as strings, without the ‘@’ prepended to them. Each of the hash’s values is the value corresponding to the given variable name.

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

widget = Widget.new('foo', 'bar')
widget.instance_values              # => {'x' => 'foo', 'y' => 'bar'}
   # File lib/reactive_support/core_ext/object/instance_variables.rb
16 def instance_values
17   Hash[instance_variables.map {|name| [name[1..-1], instance_variable_get(name) ] }]
18 end
instance_variable_names() click to toggle source

The #instance_variable_names method returns an array of the names of the instance variables defined on the calling object. The names themselves are returned as strings and, unlike in the #instance_values method, include the +‘@’+ prefix.

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

widget = Widget.new(1, 2)
widget.instance_variable_names    # => ['@x', '@y']
   # File lib/reactive_support/core_ext/object/instance_variables.rb
34 def instance_variable_names
35   instance_variables.map {|name| name.to_s }
36 end
present?() click to toggle source

When called on a generic object, the #present? method returns true if the object is present and not empty. If the object is false, empty, or nil, #present? returns false. For specific examples of different classes, see the class-specific definitions of #present?.

Time.now.present?  # => true
   # File lib/reactive_support/core_ext/object/blank.rb
28 def present? 
29   !blank?
30 end
try(*args) { |self| ... } click to toggle source

The #try method calls a given method (with given +*args+ and +&block+) on the object calling it. If the receiving object is nil, then the #try method returns nil; otherwise, #try returns the output of the method passed to it, or any error raised when the method is called. It accepts an arbitrary number of arguments and an optional block, enabling it to be used with any method.

The first argument is the name of the method to be called, given as a symbol. The rest are the arguments (if any) that should be passed into that method. Likewise, the +&block+ parameter will be passed on to the method being called.

Examples of a method being called without args:

'foo'.try(:upcase)    # => 'FOO'
nil.try(:upcase)      # => nil

Examples of a method being called with args:

%w(foo bar baz).try(:join, '.') # => 'foo.bar.baz'
nil.try(:join, '.')             # => nil

Examples of a method being called with a block:

{ foo: 10, bar: 18, baz: 32 }.try(:reject!) {|k,v| v < 25 } # => { baz: 32 }
nil.try(:reject) {|k,v| v == 'foo' }                        # => nil

When called on nil, #try returns nil even if the method being sent is defined for NilClass:

nil.try(:inspect)     # => nil

The #try method can also be called with a block and no arguments. In this case, the block will be instance_eval’ed:

'foobar'.try { upcase.truncate(3) }     # => 'FOO'
nil.try { upcase.truncate(3) }          # => nil
   # File lib/reactive_support/core_ext/object/try.rb
36 def try(*args, &block)
37   return self if self.nil?
38 
39   if args.empty? && block_given?
40     if block.arity.zero?
41       instance_eval(&block)
42     else
43       yield self
44     end
45   else
46     public_send(*args, &block) if args.respond_to?(:first)
47   end
48 end