Class: Array

Inherits:
Object show all
Defined in:
lib/core/array.rb

Instance Method Summary collapse

Instance Method Details

#count_hashObject

Returns hash mapping elements to the number of times they are found in the array.



28
29
30
31
32
33
34
35
# File 'lib/core/array.rb', line 28

def count_hash
  count = {}
  each {|e|
    count[e] ||= 0
    count[e] += 1
  }
  count
end

#exclude_all?(arr) ⇒ Boolean

Returns true if it has no elements in common with the given array.

Returns:

  • (Boolean)


99
100
101
# File 'lib/core/array.rb', line 99

def exclude_all?(arr)
  ! include_any?(arr)
end

#fmap(function) ⇒ Object

Maps the result of calling the given function name with each element as its argument.



66
67
68
69
70
# File 'lib/core/array.rb', line 66

def fmap(function)
  (function =~ /\./) ?
    map {|e| eval "#{function}(e)" } :  
    map {|e| send(function,e) }
end

#group_aoh_by_key(key, parallel_array = nil) ⇒ Object

Assuming the array is an array of hashes, this returns a hash of the elements grouped by their values for the specified hash key.



50
51
52
53
54
55
56
57
58
# File 'lib/core/array.rb', line 50

def group_aoh_by_key(key,parallel_array=nil)
  group = {}
  each_with_index {|h,i|
    value = h[key]
    group[value] = [] if ! group.has_key?(value)
    group[value].push((parallel_array.nil?) ? h : parallel_array[i])
  }
  group
end

#include_and_exclude?(do_include, dont_include = []) ⇒ Boolean

Returns true if the array includes any from the first array and excludes all from the second.

Returns:

  • (Boolean)


86
87
88
# File 'lib/core/array.rb', line 86

def include_and_exclude?(do_include,dont_include=[])
  include_any?(do_include) && exclude_all?(dont_include)
end

#include_any?(arr) ⇒ Boolean

Returns true if it has any elements in common with the given array.

Returns:

  • (Boolean)


91
92
93
94
95
# File 'lib/core/array.rb', line 91

def include_any?(arr)
  #good for large sets w/ few matches
  #! Set.new(self).intersection(arr).empty?
  arr.any? {|e| self.include?(e) }
end

#mmap(methodname, *args) ⇒ Object

Maps the result of calling each element with the given method name and optional arguments.



61
62
63
# File 'lib/core/array.rb', line 61

def mmap(methodname,*args)
  map {|e| e.send(methodname,*args) }
end

#multislice(range, splitter = ',', offset = nil) ⇒ Object

Allows you to specify ranges of elements and individual elements with one string, array starts at 1 Example: choose first and fourth through eighth elements: ‘1,4-8’



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/core/array.rb', line 4

def multislice(range,splitter=',',offset=nil)
  result = []
  for r in range.split(splitter)
  if r =~ /-/
    min,max = r.split('-')
    slice_min = min.to_i - 1
    slice_min += offset if offset
    result.push(*self.slice(slice_min, max.to_i - min.to_i + 1))
  else
    index = r.to_i - 1
    index += offset if offset
    result.push(self[index])
  end
  end
  return result
end

#permuteObject

Returns all possible paired permutations of elements disregarding order.



38
39
40
41
42
43
44
45
46
# File 'lib/core/array.rb', line 38

def permute
  permutations = []
  for i in (0 .. self.size - 1)
    for j  in (i + 1 .. self.size - 1)
      permutations.push([self[i],self[j]])
    end
  end
  permutations
end

#regindex(regex) ⇒ Object

Returns index of first element to match given regular expression.



73
74
75
76
77
78
# File 'lib/core/array.rb', line 73

def regindex(regex)
  each_with_index {|e,i|
    return i if e =~ /#{regex}/
  }
  nil
end

#replace_index(i, array) ⇒ Object

Replaces element at index with values of given array.



81
82
83
# File 'lib/core/array.rb', line 81

def replace_index(i,array)
  replace( (self[0, i] || []) +  array + self[i + 1 .. -1] )
end

#to_hashObject

Converts an even # of array elements ie [1,2,3,4] or an array of array pairs ie [[1,2],] to a hash.



23
24
25
# File 'lib/core/array.rb', line 23

def to_hash
  Hash[*self.flatten]
end