Class: Object
- Inherits:
- BasicObject
- Includes:
- ReactiveExtensions
- Defined in:
- lib/reactive_support/core_ext/object/blank.rb,
lib/extensions/reactive_extensions.rb,
lib/reactive_support/core_ext/object/try.rb,
lib/reactive_support/core_ext/object/exist.rb,
lib/reactive_support/core_ext/object/deep_dup.rb,
lib/reactive_support/core_ext/object/inclusion.rb,
lib/reactive_support/core_ext/object/duplicable.rb,
lib/reactive_support/extensions/reactive_extensions.rb,
lib/reactive_support/core_ext/object/instance_variables.rb
Overview
Instance Method Summary collapse
-
#blank? ⇒ Boolean
When called on a generic object, the
#blank?method returnstrueif the object is false, empty, or nil. -
#deep_dup ⇒ Object
The
#deep_dupmethod returns a duplicate of a duplicable object. -
#duplicable? ⇒ Boolean
The
#duplicable?method checks whether an object may be safely duplicated. -
#exists? ⇒ Boolean
(also: #exist?)
The
#exists?method and its alias,#exist?, returntrueif the object calling the method is notnil. -
#in?(object) ⇒ Boolean
The
#in?method returnstrueif the calling object is included in theobjectpassed as a parameter. -
#instance_values ⇒ Object
instance variable names to the variables’ values.
-
#instance_variable_names ⇒ Object
The
#instance_variable_namesmethod returns an array of the names of the instance variables defined on the calling object. -
#present? ⇒ Boolean
When called on a generic object, the
#present?method returnstrueif the object is present and not empty. -
#try(*args, &block) ⇒ Object
The
#trymethod calls a given (with given *args and &block) on the object calling it.
Methods included from ReactiveExtensions
Instance Method Details
#blank? ⇒ Boolean
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
18 19 20 |
# File 'lib/reactive_support/core_ext/object/blank.rb', line 18 def blank? respond_to?(:empty) ? !!empty : !self end |
#deep_dup ⇒ Object
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.
14 15 16 |
# File 'lib/reactive_support/core_ext/object/deep_dup.rb', line 14 def deep_dup duplicable? ? self.dup : self end |
#duplicable? ⇒ Boolean
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.
8 9 10 |
# File 'lib/reactive_support/core_ext/object/duplicable.rb', line 8 def duplicable? true end |
#exists? ⇒ Boolean Also known as: exist?
The #exists? method and its alias, #exist?, return true if the object calling the method is not nil.
'foobar'.exists? # => true
nil.exists? # => false
9 10 11 |
# File 'lib/reactive_support/core_ext/object/exist.rb', line 9 def exists? !self.nil? end |
#in?(object) ⇒ Boolean
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.
34 35 36 37 38 |
# File 'lib/reactive_support/core_ext/object/inclusion.rb', line 34 def in?(object) object.include?(self) rescue NoMethodError raise ArgumentError.new("The parameter passed to #in? must respond to #include?") end |
#instance_values ⇒ Object
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.new('foo', 'bar')
.instance_values # => {'x' => 'foo', 'y' => 'bar'}
16 17 18 |
# File 'lib/reactive_support/core_ext/object/instance_variables.rb', line 16 def instance_values Hash[instance_variables.map {|name| [name[1..-1], instance_variable_get(name) ] }] end |
#instance_variable_names ⇒ Object
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.new(1, 2)
.instance_variable_names # => ['@x', '@y']
34 35 36 |
# File 'lib/reactive_support/core_ext/object/instance_variables.rb', line 34 def instance_variable_names instance_variables.map {|name| name.to_s } end |
#present? ⇒ Boolean
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
28 29 30 |
# File 'lib/reactive_support/core_ext/object/blank.rb', line 28 def present? !blank? end |
#try(*args, &block) ⇒ Object
The #try method calls a given (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
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/reactive_support/core_ext/object/try.rb', line 35 def try(*args, &block) return self if self.nil? if args.empty? && block_given? if block.arity.zero? instance_eval(&block) else yield self end else public_send(*args, &block) if args.respond_to?(:first) end end |