Class: Array

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

Instance Method Summary collapse

Instance Method Details

#first_duplicateObject



41
42
43
44
# File 'lib/ditzstr/util.rb', line 41

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

#flatten_one_levelObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/ditzstr/util.rb', line 50

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

#listify(prefix = "") ⇒ Object



23
24
25
26
27
28
# File 'lib/ditzstr/lowline.rb', line 23

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

#to_hObject



46
47
48
# File 'lib/ditzstr/util.rb', line 46

def to_h
  Hash[*flatten]
end

#uniq_byObject



12
# File 'lib/ditzstr/util.rb', line 12

def uniq_by; inject({}) { |h, o| h[yield(o)] = o; h }.values end