Class: Array

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

Instance Method Summary collapse

Instance Method Details

#first_duplicateObject



33
34
35
36
# File 'lib/util.rb', line 33

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

#flatten_one_levelObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/util.rb', line 42

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

#listify(prefix = "") ⇒ Object



30
31
32
33
34
35
# File 'lib/lowline.rb', line 30

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

#to_hObject



38
39
40
# File 'lib/util.rb', line 38

def to_h
  Hash[*flatten]
end