Class: Array

Inherits:
Object show all
Defined in:
lib/util.rb,
lib/lowline.rb

Instance Method Summary collapse

Instance Method Details

#first_duplicateObject



25
26
27
28
# File 'lib/util.rb', line 25

def first_duplicate
  sa = sort
  (1 .. sa.length).argfind { |i| (sa[i] == sa[i - 1]) && sa[i] }
end

#flatten_one_levelObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/util.rb', line 34

def flatten_one_level
  inject([]) do |ret, e|
    case e
    when Array
      ret + e
    else
      ret << e
    end
  end
end

#listify(prefix = "") ⇒ Object



25
26
27
28
29
30
# File 'lib/lowline.rb', line 25

def listify prefix=""
  return "" if empty?
  "\n" +
    map_with_index { |x, i| x.to_s.gsub(/^/, "#{prefix}#{i + 1}. ") }.
      join("\n")
end

#to_hObject



30
31
32
# File 'lib/util.rb', line 30

def to_h
  Hash[*flatten]
end