Class: Object

Inherits:
BasicObject
Defined in:
lib/sinclair/core_ext/object.rb

Overview

Extension to core class Object

Author:

  • darthjee

Instance Method Summary collapse

Instance Method Details

#is_any?(*classes) ⇒ TrueClass, FalseClass

Checks if an object is an instance of any of the given classes

Examples:

object = [1, 2, 3]

object.is_any?(Hash, Class) # returns false
object.is_any?(Hash, Array) # returns true

Parameters:

  • classes (Array<Class>)

    classes to be checked against object

Returns:

  • (TrueClass, FalseClass)


22
23
24
# File 'lib/sinclair/core_ext/object.rb', line 22

def is_any?(*classes)
  classes.any?(method(:is_a?))
end

#map_and_find {|*args| ... } ⇒ ::Object

Maps the elements into a new value, returning only one

The result to be returned is the first mapping that is evaluated to true

This method is equivalent to #map#find but only calling the map block up to when a value is found

Examples:

Using an array of keys to remove remove elements of a hash


service_map = {
  a: nil,
  b: false,
  c: 'found me',
  d: nil,
  e: 'didnt find me'
}

keys = %i[a b c d e]

keys.map_and_find do |key|   #
  service_values.delete(key) #
end                          # returns 'found me'

service_map # has lost only 3 keys returning
            # { d: nil, e: 'didnt find me' }

Yields:

  • (*args)

    mappig block

Returns:



58
59
60
61
62
63
64
# File 'lib/sinclair/core_ext/object.rb', line 58

def map_and_find
  mapped = nil
  find do |*args|
    mapped = yield(*args)
  end
  mapped || nil
end