Module: Enumerable
- Defined in:
- lib/initializers/enumerable_changes.rb
Instance Method Summary collapse
- #dups ⇒ Object
-
#smart_sort ⇒ Object
Sorts all items in this enumerable first by non-digits and then by digits.
Instance Method Details
#dups ⇒ Object
26 27 28 29 |
# File 'lib/initializers/enumerable_changes.rb', line 26 def dups tmp_dups = inject({}){|h,v| h[v]=h[v].to_i+1; h}.select{|k,v| v > 1} tmp_dups.is_a?(Hash) ? tmp_dups.keys : tmp_dups end |
#smart_sort ⇒ Object
Sorts all items in this enumerable first by non-digits and then by digits.
If a block is given, it will be invoked to calculate the basis for comparison. Otherwise, the items themselves will be used as the basis for comparison.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/initializers/enumerable_changes.rb', line 10 def smart_sort # :yields: item sort_by do |item| basis = if block_given? yield item else item end.to_s alpha = basis.scan(/\D+/) digit = basis.scan(/\d+/).map(&:to_i) [alpha, digit] end end |