Module: WhoCalledMe::Utils::ArrayUtils

Defined in:
lib/who_called_me/utils/array_utils.rb

Instance Method Summary collapse

Instance Method Details

#first_elements_with_common_prefix(arr, pre_len_to_ignore = 0) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/who_called_me/utils/array_utils.rb', line 10

def first_elements_with_common_prefix(arr, pre_len_to_ignore=0)
  arr.each_index do |i|
    # exclude the n first characters from the comparison <- remove them
    arr[i] =  arr[i][pre_len_to_ignore..-1]
  end

  [].tap do |results|
    first_line = arr.first
    results << first_line
    (0..arr.length-2).each do |i|
      line, next_line = arr[i], arr[i+1]

      next_line_in_same_code_tree = StringUtils.have_common_substring?(line, next_line)
      next_line_in_same_code_tree ?
        (results << next_line) :
        break
    end
  end
end

#indent(array, options = {:prefix => nil}) ⇒ Object



5
6
7
8
# File 'lib/who_called_me/utils/array_utils.rb', line 5

def indent(array, options={:prefix => nil})
  prefix = options[:prefix]
  array.collect{|l| "#{prefix}#{l}"}
end