A library of utility services and concerns.
Low-level tools for working with objects.
.instance => SleepingKingStudios::Tools::Base
#apply(receiver, proc, *args, **kwargs, &block) =>
Calls a Proc or lambda on the given receiver with the given parameters.
Unlike calling #instance_exec with the block, ObjectTools#apply allows you to specify a block parameter.
my_object = double('object', :to_s => 'A mock object')
my_proc = ->() { puts %{#{self.to_s} says "Greetings, programs!"} }
ObjectTools.apply my_object, my_proc
#=> Writes 'A mock object says "Greetings, programs!"' to STDOUT.
#deep_dup(obj) =>
Creates a deep copy of the object.
If the object is an Array, returns a new Array with deep copies of each array item. If the object is a Hash, returns a new Hash with deep copies of each hash key and value. Otherwise, returns Object#dup.
data = {
:songs = [
{
:name => 'Welcome to the Jungle',
:artist => "Guns N' Roses",
:album => 'Appetite for Destruction'
},
{
:name => 'Hells Bells',
:artist => 'AC/DC',
:album => 'Back in Black'
},
{
:name => "Knockin' on Heaven's Door",
:artist => 'Bob Dylan',
:album => 'Pat Garrett & Billy The Kid'
}
]
}
copy = ObjectTools.deep_dup data
copy[:songs] << { :name => 'Sympathy for the Devil', :artist => 'The Rolling Stones', :album => 'Beggars Banquet' }
data[:songs].count
#=> 3
copy[:songs][1][:name] = 'Shoot to Thrill'
data[:songs][1]
#=> { :name => 'Hells Bells', :artist => 'AC/DC', :album => 'Back in Black' }
#deep_freeze(obj) => Object
Performs a deep freeze of the object.
If the object is an Array, freezes the array and performs a deep freeze on each array item. If the object is a hash, freezes the hash and performs a deep freeze on each hash key and value. Otherwise, calls Object#freeze.
data = {
:songs = [
{
:name => 'Welcome to the Jungle',
:artist => "Guns N' Roses",
:album => 'Appetite for Destruction'
},
{
:name => 'Hells Bells',
:artist => 'AC/DC',
:album => 'Back in Black'
},
{
:name => "Knockin' on Heaven's Door",
:artist => 'Bob Dylan',
:album => 'Pat Garrett & Billy The Kid'
}
]
}
ObjectTools.deep_freeze(data)
data.frozen?
#=> true
data[:songs].frozen?
#=> true
data[:songs][0].frozen?
#=> true
data[:songs][0].name.frozen?
#=> true
#dig(obj, *method_names) => Object, nil
Accesses deeply nested attributes on an object.
This method calls the first named method on the given object, and then each subsequent method on the result of the previous method call. If the object does not respond to the method name, nil is returned instead of calling the method.
ObjectTools.dig my_object, :first_method, :second_method, :third_method
#=> my_object.first_method.second_method.third_method
#eigenclass(obj) => Class
Also known as:
metaclass
Returns the object’s eigenclass.
#immutable?(obj) => Boolean
Checks if the object is immutable.
ObjectTools.immutable?(nil)
#=> true
ObjectTools.immutable?(false)
#=> true
ObjectTools.immutable?(0)
#=> true
ObjectTools.immutable?(:hello)
#=> true
ObjectTools.immutable?('Greetings, programs!')
#=> true
ObjectTools.immutable?(+'Greetings, programs!')
#=> false
ObjectTools.immutable?([1, 2, 3])
#=> false
ObjectTools.immutable?([1, 2, 3].freeze)
#=> false
#mutable?(obj) => Boolean
Checks if the object is mutable.
#object?(obj) => Boolean
Returns true if the object is an Object.
This should return false only for objects that have an alternate inheritance chain from BasicObject, such as a Proxy.
ObjectTools.object?(nil)
#=> true
ObjectTools.object?([])
#=> true
ObjectTools.object?({})
#=> true
ObjectTools.object?(1)
#=> true
ObjectTools.object?(BasicObject.new)
#=> false
#try(obj, method_name, *args) => Object, nil
As #send, but returns nil if the object does not respond to the method.
This method relies on #respond_to?, so methods defined with method_missing will not be called.
ObjectTools.try(%w(ichi ni san), :count)
#=> 3
ObjectTools.try(nil, :count)
#=> nil
Back to Documentation | Reference | SleepingKingStudios | SleepingKingStudios::Tools