From: Robert Klemme Date: 2011-09-12T18:35:23+09:00 Subject: Re: making an array of strings On Mon, Sep 12, 2011 at 3:19 AM, 7stud -- wrote: > Joe Collins wrote in post #1021260: >> Also, I want to read words from a file and make them an array of >> strings. I can read files fine but making an array of strings is where I >> am >> stumped. >> > > text.txt: ---- > > hello world > dog > boy girl child > cat > ---------- > > > File.open('text.txt') do |f| >  p f.read.split.sort > end > > --output:-- > ["boy", "cat", "child", "dog", "girl", "hello", "world"] I'd do words = [] File.foreach "text.txt" do |line| words.concat(line.scan(/\w+/)) end Joe, if you want to avoid duplicates you can use Set or Hash: # Hash words = {} File.foreach "text.txt" do |line| line.scan(/\w+/) {|word| words[word] = true} end words = words.keys # Set require 'set' words = Set.new File.foreach "text.txt" do |line| words.merge(line.scan(/\w+/)) end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/