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

···

--
Posted via http://www.ruby-forum.com/.

Joe Collins 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] class String - RDoc Documentation
[2] http://www.rubycentral.com/pickaxe/

···

Thanks in advance

Joe

--
Posted via http://www.ruby-forum.com/\.

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)

···

--
Posted via http://www.ruby-forum.com/\.

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

···

--
Posted via http://www.ruby-forum.com/.

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"]

--
Posted via http://www.ruby-forum.com/\.

Joe Collins 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.

2) %w(BOY\ HOOD girl cat dog)

···

--
Posted via http://www.ruby-forum.com/\.

# 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:58 AM, Zipizap zipizap <zipizap123@gmail.com>wrote:

On Sun, Sep 11, 2011 at 9:00 AM, Joe Collins <joec_49@hotmail.com> 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.

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

···

On Mon, Sep 12, 2011 at 3:19 AM, 7stud -- <bbxx789_05ss@yahoo.com> 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"]

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/