syntax - How to make a word list from a text file in a more Ruby way? -
i'd word list text file using ruby. found how use regex parse words here, made script following:
src = file.open("text.txt") word_list = [] src.each |line| word_list << line.downcase.split(/[^[:alpha:]]/).delete_if {|x| x == ""} end word_list.flatten!.uniq!.sort! p word_list and following sample text file text.txt:
textmate may latest craze developing ruby on rails applications, vim forever. plugin offers following features ruby on rails application development.
automatically detects buffers containing files rails applications, , applies settings buffers (and buffers). can use autocommand apply own custom settings well.
unintrusive. files in rails application should affected; regular ruby scripts left untouched. when enabled, plugin should keep out of way if you're not using features.
easy navigation of rails directory structure. gf considers context , knows partials, fixtures, , more. there 2 commands, :a (alternate) , :r (related) easy jumping between files, including favorites model migration, template helper, , controller functional test. more advanced usage, :rmodel, :rview, :rcontroller, , several other commands provided.
as ruby novice, i'd learn better (more clear, concise, , following conventions) solutions problem.
thanks advices , corrections.
a more idiomatic code be:
word_list = open("text.txt") .lines .flat_map { |line| line.downcase.split(/[^[:alpha:]]/).reject(&:empty?) } .uniq .sort
Comments
Post a Comment