-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpokedex.rb
41 lines (36 loc) · 832 Bytes
/
pokedex.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
require 'json'
require 'open-uri'
require_relative 'pokemon'
class Pokedex
def initialize(file_name)
@all = []
@map = {}
JSON.parse(open(file_name).read).map do |p|
pokemon = Pokemon.new(p)
@all << pokemon
pokemon.unique_chars.each do |char|
if @map.include?(char)
@map[char] << pokemon unless @map[char].include?(pokemon)
else
@map[char] = [pokemon]
end
end
end
@map = Hash[@map.sort_by { |_, v| v.length }]
@map.each do |k, v|
@map[k] = v.sort { |a, b| (a.usefulness(chars_order_map) <=> b.usefulness(chars_order_map))}
end
end
def all
@all
end
def map
@map
end
def chars_order
@keys ||= @map.keys
end
def chars_order_map
@chars_order_map ||= Hash[@map.map{|x, i| [x, i.length]}]
end
end