Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/monkey_patches.rb

Overview

Add methods to Array so that one attribute of a class can be treated as a unique key

Instance Method Summary collapse

Instance Method Details

#keyfind(key) ⇒ Object

Returns The object from the array that has the specified key value.

Parameters:

  • key (Object)

    The key value to search for

Returns:

  • (Object)

    The object from the array that has the specified key value



63
64
65
66
# File 'lib/monkey_patches.rb', line 63

def keyfind(key)
  self.each { |e| return e if key == e.key }
  nil
end

#keysArray<Object>

Returns An array containing the key fields from a collection of objects.

Returns:

  • (Array<Object>)

    An array containing the key fields from a collection of objects



57
58
59
# File 'lib/monkey_patches.rb', line 57

def keys
  self.collect { |e| e.key }
end