Ruby: constant, module, hash -
i'm few months learning ruby, , right i'm trying build south korean/north korean/english dictionary type of thing. i'm feeding text file has words.
so far i've got:
module dictionary dictionary = [] end class file include dictionary def self.convert(file) readlines(file).each |line| south, north, meaning = line.split(',') dictionary << { :south => south, :north => north, :meaning => meaning } end end end file.convert("dictionary.txt") dictionary::dictionary.sort_by { |word| word[:north] }.each |word| puts "#{word[:south]} #{word[:north]} in north korean. both mean #{word[:meaning]}" end
my question is:
1) unnecessary me make separate module array? (i trying experiment mixing in modules , classes)
2) using constant array right move? guess thought process wanted array able accessed outside, honest don't know i'm doing.
thanks in advance.
since dictionary loaded file, it's better have class instead of module each file can parsed separate dictionary.
class dictionary attr_reader :content def initialize @content = [] end def self.load(path) instance = new file.open(path) |f| f.each_line |line| instance.content << %i(south, north, meaning).zip(line.split(',')) end end instance end end
besides, can see didn't patch file
class because file
not creating dictionaries, sorts of file manipulation.
Comments
Post a Comment