Making an array of strings

I want to make an array of strings, i.e boy, girl, cat dog
how do I do that in Ruby?

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.

Thanks in advance

Joe

Joe C. wrote in post #1021260:

I want to make an array of strings, i.e boy, girl, cat dog
how do I do that in Ruby?

#You can do it several ways:

A- Create an array which elements are strings

str_arr = [“boy”, “girl”, “cat”, “dog”] # => [“boy”, “girl”, “cat”,
“dog”]

B- The same using %w - it splits a string into words, and returns an

array. Notice also that () is used to delimit the string, instead of “”
str_arr = %w(boy girl cat dog) # => [“boy”, “girl”, “cat”, “dog”]

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.

#To decompose a string into words, and store them in an array, you can
use String#split [1] - it fits like a glove

Also, if you want to refresh these details, have a reading over “The
Pragmatic Programmer’s Guide” which is available online, and very well
explained [2]

Cheers

[1] http://ruby-doc.org/core/classes/String.html#M001165
[2] http://www.rubycentral.com/pickaxe/

Thanks in advance

Joe

I want to make an array of strings, i.e boy, girl, cat dog
how do I do that in Ruby?

array = %w( boy girl cat dog )

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.

location_of_your_file_goes_here = ‘/tmp/foobar.txt’
array = File.readlines(location_of_your_file_goes_here)

Thanks - works great…how would I used %w to catch strings including
blanks. I tried %w(‘BOY HOOD’,girl,cat dog) but that didn’t work.

Joe

On Sun, Sep 11, 2011 at 9:58 AM, Zipizap zipizap
[email protected]wrote:

B- The same using %w - it splits a string into words, and returns an

array. Notice also that () is used to delimit the string, instead of “”
str_arr = %w(boy girl cat dog) # => [“boy”, “girl”, “cat”, “dog”]

The delimiters are arbitrary, so you can use quotes if you want.

%w"boy girl cat dog" # => [“boy”, “girl”, “cat”, “dog”]

On Sun, Sep 11, 2011 at 9:00 AM, Joe C. [email protected]
wrote:

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.

My answer would change depending on some variables like how big your
file is
and whether the words are each on their own line or if there is one line
with spaces between each, or if there are lots of lines with lots of
words
one each, so might be beneficial to provide more information.

Joe C. 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”]

On Mon, Sep 12, 2011 at 3:19 AM, 7stud – [email protected]
wrote:

dog
[“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

Joe C. wrote in post #1021281:

Thanks - works great…how would I used %w to catch strings including
blanks. I tried %w(‘BOY HOOD’,girl,cat dog) but that didn’t work.

  1. Get rid of the commas. Whitespace separates words.

p %w(BOY\ HOOD girl cat dog)

–output:–
[“BOY HOOD”, “girl”, “cat”, “dog”]

Or,

sentence = “Here we go again.”

p %W[
#{sentence}
hello
world
]

–output:–
[“Here we go again.”, “hello”, “world”]

(Note the capitalized: %W)