Module: Core::Array

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.



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

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)


101
102
103
# File 'lib/core/array.rb', line 101

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.



68
69
70
71
72
# File 'lib/core/array.rb', line 68

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.



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

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)


88
89
90
# File 'lib/core/array.rb', line 88

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)


93
94
95
96
97
# File 'lib/core/array.rb', line 93

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.



63
64
65
# File 'lib/core/array.rb', line 63

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’



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

def multislice(range,splitter=',',offset=nil)
  #td: fix swallowing of empty lines
  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.



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

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.



75
76
77
78
79
80
# File 'lib/core/array.rb', line 75

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.



83
84
85
# File 'lib/core/array.rb', line 83

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.



25
26
27
# File 'lib/core/array.rb', line 25

def to_hash
  Hash[*self.flatten]
end