Class: Array

Inherits:
Object show all
Defined in:
lib/picky/extensions/array.rb

Overview

The Array class we all know and love.

Instance Method Summary collapse

Instance Method Details

#clustered_uniqObject

Around 10% faster than the above.

Returns a copy.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/picky/extensions/array.rb', line 9

def clustered_uniq
  result = []
  self.inject(nil) do |last, element|
    if last == element
      last
    else
      result << element && element
    end
  end
  result
end

#sort_by_levenshtein!(from) ⇒ Object

Sort the array using distance from levenshtein.

Will raise if encounters not to_s-able element.



25
26
27
28
29
30
# File 'lib/picky/extensions/array.rb', line 25

def sort_by_levenshtein! from
  from = from.to_s
  sort! do |this, that|
    Text::Levenshtein.distance(this.to_s, from) <=> Text::Levenshtein.distance(that.to_s, from)
  end
end