Module: Enumerable

Includes:
JishoSort::Sortable
Defined in:
lib/jisho_sort/enumerable.rb

Instance Method Summary collapse

Methods included from JishoSort::Sortable

#compare_by_furigana

Instance Method Details

#jisho_sort {|a, b| ... } ⇒ Array<String>

Sorts an array of strings using the Japanese pronunciation (dictionary)order. If a block is given, it sorts using the provided block.

Yields:

  • (a, b)

    Optional block to customize the sorting logic.

Yield Parameters:

  • a (String)

    The first string to compare.

  • b (String)

    The second string to compare.

Returns:

  • (Array<String>)

    The sorted array.

Raises:

  • (ArgumentError)

    If no block is given and any element is not a String.



14
15
16
17
18
19
20
# File 'lib/jisho_sort/enumerable.rb', line 14

def jisho_sort(&block)
  raise ArgumentError if block.nil? && !all? { |item| item.instance_of?(String) }

  return sort { |a, b| a.compare_by_furigana(b) } if block.nil?

  sort(&block)
end

#jisho_sort_by {|item| ... } ⇒ Array

Sorts the elements of the enumerable based on the japanese pronunciation of the strings returned by the given block.

Yields:

  • (item)

    Gives each element of the enumerable to the block.

Yield Returns:

  • (String)

    The string whose japanese pronunciation will be used for sorting.

Returns:

  • (Array)

    A new array with the elements sorted by the japanese pronunciation of the strings.

Raises:

  • (ArgumentError)

    If any element does not yield a String.



29
30
31
32
33
# File 'lib/jisho_sort/enumerable.rb', line 29

def jisho_sort_by
  raise ArgumentError unless all? { |item| yield(item).instance_of?(String) }

  sort_by{ |item| yield(item).furigana }
end