Class: ArrayChinese

Inherits:
Array
  • Object
show all
Defined in:
lib/a-b-chi.rb

Instance Method Summary collapse

Constructor Details

#initializeArrayChinese

Returns a new instance of ArrayChinese.



5
6
7
8
9
10
11
# File 'lib/a-b-chi.rb', line 5

def initialize
  spec = Gem::Specification.find_by_name("a-b-chi")
  gem_root = spec.gem_dir
  gem_lib = gem_root + "/lib"
  @characters = File.read("#{gem_lib}/characters.txt").split("\n")

end

Instance Method Details

#sortObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/a-b-chi.rb', line 13

def sort
  max_length = self.max_by(&:length).length
  search_query = '['
  for i in 0..max_length
    unless i == max_length
      search_query  << "@characters.index(sort_string[#{i}]), "
    else
      search_query  << "@characters.index(sort_string[#{i}])]"
    end
  end
  self.sort_by! { |sort_string| eval(search_query) }
end