# Help reading data from a file

**URL:** <https://www.ruby-forum.com/t/help-reading-data-from-a-file/160221>\
**Category:** Ruby\
**Created:** [February 13, 2009, 5:46am UTC](https://www.ruby-forum.com/t/help-reading-data-from-a-file/160221 "2009-02-13T05:46:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [February 13, 2009, 5:46am UTC](https://www.ruby-forum.com/t/help-reading-data-from-a-file/160221/1 "2009-02-13T05:46:54Z")

</div>

I have a ruby program that I use to generate text files from a single  
array’s data, but what I would like to do is be able to have it generate  
files from multiple arrays and save them in a different folder when it  
starts writing files from a different array.

One problem I’m facing is how to read an array from a file. If I have a  
file with an only an array in it, how do I apply a method such as  
“.generate” to the array? Do I name the array inside the file, such as  
“array1” and then apply the method “.generate”? Or can it be applied to  
the file itself, such as “file1.generate”?

cities = [‘Nashville’,  
‘Orlando’,  
‘Tampa’  
‘Memphis’  
]

cities.each do |city|  
output = %Q(#{city})

######################################

# 

#Saves File.

# 

######################################

```
File::open("C:\\Documents And

```

Settings\Administrator\Desktop\city\#{city.downcase.delete(’  
')}.txt", ‘w’) do |f|  
f.write output  
end  
end

I’m not sure of the best way to expand this code to have it generate  
files for multiple arrays and have each array’s generated files be saved  
in a separate folder instead of all of them together. To keep from  
having 1 huge file, I’d like to have each array in a different file, but  
if this isn’t the best way to do it, can someone give me a better  
example…?

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [February 13, 2009, 7:10am UTC](https://www.ruby-forum.com/t/help-reading-data-from-a-file/160221/2 "2009-02-13T07:10:42Z")

</div>

Zoe P. wrote:

> One problem I’m facing is how to read an array from a file.

puts IO.read(“data2”)  
Nashville  
Orlando  
Tampa  
Memphis  
==\>nil

p IO.readlines(“data2”)  
[“Nashville\n”, “Orlando\n”, “Tampa\n”, “Memphis\n”]  
==\>nil

p IO.readlines(“data2”).map{|s| s.strip}  
[“Nashville”, “Orlando”, “Tampa”, “Memphis”]  
==\>nil

p IO.read(“data2”).split( /\s\*\n\s\*/ )  
[“Nashville”, “Orlando”, “Tampa”, “Memphis”]  
==\>nil

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [February 13, 2009, 3:28pm UTC](https://www.ruby-forum.com/t/help-reading-data-from-a-file/160221/3 "2009-02-13T15:28:15Z")

</div>

William J. wrote:

> Zoe P. wrote:
> 
> > One problem I’m facing is how to read an array from a file.
> 
> puts IO.read(“data2”)  
> Nashville  
> Orlando  
> Tampa  
> Memphis  
> ==\>nil
> 
> p IO.read(“data2”).split( /\s\*\n\s\*/ )  
> [“Nashville”, “Orlando”, “Tampa”, “Memphis”]  
> ==\>nil

puts IO.read(“data2.txt”)

–output:–  
Nashville  
Orlando  
Tampa  
Memphis

p IO.read(“data2.txt”).split()

–output:–  
[“Nashville”, “Orlando”, “Tampa”, “Memphis”]

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [February 13, 2009, 5:09pm UTC](https://www.ruby-forum.com/t/help-reading-data-from-a-file/160221/4 "2009-02-13T17:09:31Z")

</div>

Zoe P. wrote:

> One problem I’m facing is how to read an array from a file. If I have a  
> file with only an array in it, how do I apply a method such as  
> “.generate” to the array?

arr = %w{Nashville Orlando Tampa Memphis}

File.open(“data.txt”, “w”) do |f|  
Marshal.dump(arr, f)  
end

cities = nil

File.open(“data.txt”) do |f|  
cities = Marshal.load(f)  
end

p cities  
p cities.length  
p cities.reverse

–output:–  
[“Nashville”, “Orlando”, “Tampa”, “Memphis”]  
4  
[“Memphis”, “Tampa”, “Orlando”, “Nashville”]

> cities = [‘Nashville’,  
> ‘Orlando’,  
> ‘Tampa’  
> ‘Memphis’  
> ]
> 
> cities.each do |city|  
> output = %Q(#{city})  
> dir = “/some/path/#{city.downcase.delete(’ ')}.txt”
> 
> ```
> File::open(dir, 'w') do |f|
> f.write output
> end
> 
> ```
> 
> end
> 
> I have a ruby program that I use to generate text files from a single  
> array’s data, but what I would like to do is be able to have it generate  
> files from multiple arrays and save them in a different folder when it  
> starts writing files from a different array.

data = {  
‘cities’ =\> %w{Nasheville Olrando Tampa Memphis},  
‘dogs’ =\> %w{small medium large},  
‘friends’ =\> %w{Dave Cathy Jill}  
}

dirs = {  
‘cities’ =\> ‘./a’, #Must be existing dirs  
‘dogs’ =\> ‘./b’,  
‘friends’ =\> ‘./c’  
}

data.each do |key, val|  
current\_path = “#{dirs[key]}/#{key}”

File.open(current\_path, “w”) do |f|  
val.each do |arr\_element|  
f.write "#{arr\_element} "  
end  
end

end

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [March 11, 2009, 4:26pm UTC](https://www.ruby-forum.com/t/help-reading-data-from-a-file/160221/5 "2009-03-11T16:26:00Z")

</div>

7stud – wrote:

> 7stud – wrote:
> 
> > File.open(current\_path, “w”) do |f|  
> > val.each do |arr\_element|  
> > f.write "#{arr\_element} "  
> > end  
> > end
> 
> That could be simply:
> 
> File.open(current\_path, “w”) do |f|  
> f.write val.join(" ")  
> end

Do either of you know anyone with RoR experience & and active security  
clearance? We have a contract opening with flexible pay in Tampa, FL.  
$1000 referral bonus. Please let me know (removed\_email\_address@domain.invalid).

Thanks!  
Cory

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [February 13, 2009, 5:15pm UTC](https://www.ruby-forum.com/t/help-reading-data-from-a-file/160221/6 "2009-02-13T17:15:02Z")

</div>

7stud – wrote:

> File.open(current\_path, “w”) do |f|  
> val.each do |arr\_element|  
> f.write "#{arr\_element} "  
> end  
> end

That could be simply:

File.open(current\_path, “w”) do |f|  
f.write val.join(" ")  
end
