# Ruby Newbie...Classes and Objects, oh my!

**URL:** <https://www.ruby-forum.com/t/ruby-newbie-classes-and-objects-oh-my/189105>\
**Category:** Ruby\
**Created:** [May 24, 2010, 4:31pm UTC](https://www.ruby-forum.com/t/ruby-newbie-classes-and-objects-oh-my/189105 "2010-05-24T16:31:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fuglyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9dc877/32.png) [@fuglyducky](https://www.ruby-forum.com/u/fuglyducky)\
**Post date:** [May 24, 2010, 4:31pm UTC](https://www.ruby-forum.com/t/ruby-newbie-classes-and-objects-oh-my/189105/1 "2010-05-24T16:31:02Z")

</div>

I’m very new to Ruby and I’m trying to create a program that will take  
one text file and dump each line into an array. Then I take a second  
file and dump that into a string. Then I insert the string into the  
array. The code for these pieces work. However, for flexibility I want/  
need to be able to use OO methods and that is where I seem to hit a  
wall. Not sure what I am supposed to do or how to call it. Below is my  
code, any input you may have would be greatly appreciated!!!

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

class XMLProcessing  
def initialize(array\_file, string\_file, output\_file)  
@array\_file = array\_file  
@string\_file = string\_file  
@output\_file = output\_file  
end

def lines\_to\_array  
# Dump each line from text file into array  
File.open(@array\_file).each do |line|  
my\_array \<\< line  
end  
end

def file\_to\_string  
# Create string from text file  
@my\_string\_file = File.open(@string\_file)  
my\_string = @my\_string\_file.read  
end

def string\_into\_array  
# Insert string into array  
my\_array.insert((my\_array.length - 1), my\_string)  
end

def new\_array\_to\_file  
# Dump the array to a text file  
newFile = File.new(@output\_file, “w+”)  
my\_array.each { |element| newFile.puts element }  
print 'New file created: ’ + (@output\_file)  
end  
end

stat\_xml = XMLProcessing.new(“Stat.Template.xml”, “Seg.Template.xml”,  
“stat.xml.txt”)

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

---

<div class="post-metadata">

**Author:** ![fuglyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9dc877/32.png) [@fuglyducky](https://www.ruby-forum.com/u/fuglyducky)\
**Post date:** [May 24, 2010, 5:58pm UTC](https://www.ruby-forum.com/t/ruby-newbie-classes-and-objects-oh-my/189105/2 "2010-05-24T17:58:15Z")

</div>

On Mon, May 24, 2010 at 4:30 PM, fuglyducky [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> class XMLProcessing  
> def initialize(array\_file, string\_file, output\_file)  
> @array\_file = array\_file  
> @string\_file = string\_file  
> @output\_file = output\_file  
> end
> 
> ```
> def lines_to_array
> # Dump each line from text file into array
> File.open(@array_file).each do |line|
> my_array << line
> 
> ```

This is a local variable, uninitialized for that matter  
try  
@my\_array = []  
File.open…  
@my\_array \<\< line  
end

However, you could also simply do a  
@my\_array = File.readlines( @array\_file ).map( &:chomp )  
or the more explicit (and in Rubies \< 1.9 necessary )  
@my\_array = File.readlines( @array\_file ).map{ |line| line.chomp }

> ```
> end
> end
> 
> def file_to_string
> # Create string from text file
> @my_string_file = File.open(@string_file)
> my_string = @my_string_file.read
> 
> ```

Same here  
@my\_string = …

> ```
> end
> 
> def string_into_array
> # Insert string into array
> my_array.insert((my_array.length - 1), my_string)
> 
> ```

and here too  
@my\_array

> ```
> end
> 
> def new_array_to_file
> # Dump the array to a text file
> newFile = File.new(@output_file, "w+")
> my_array.each { |element| newFile.puts element }
> 
> ```

change here too  
@my\_array

> ```
> print 'New file created: ' + (@output_file)
> end
> 
> ```
> 
> end
> 
> stat\_xml = XMLProcessing.new(“Stat.Template.xml”, “Seg.Template.xml”,  
> “stat.xml.txt”)

Now just call the methods you want to execute on stat\_xml as e.g.  
stat\_xml.file\_to\_string  
…  
…

I do not think your design is ideal, calling all those external method  
calls for intermediate computations(1), but for a beginner it is  
pretty nicely structured code.

HTH  
R.

(1) But there might be reasons invisible to YHS.

---

<div class="post-metadata">

**Author:** ![fuglyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9dc877/32.png) [@fuglyducky](https://www.ruby-forum.com/u/fuglyducky)\
**Post date:** [May 24, 2010, 6:45pm UTC](https://www.ruby-forum.com/t/ruby-newbie-classes-and-objects-oh-my/189105/3 "2010-05-24T18:45:40Z")

</div>

On May 24, 8:57 am, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > my_array << line
> > 
> > ```
> 
> or the more explicit (and in Rubies \< 1.9 necessary )
> 
> > ```
> > # Dump the array to a text file
> > 
> > ```
> > 
> > stat\_xml = XMLProcessing.new(“Stat.Template.xml”, “Seg.Template.xml”,
> 
> The best way to predict the future is to invent it.  
> – Alan Kay

Wow…thanks for the response!!! I’m not a programmer for a developer  
by any means. I’m a tester that needed a quick (and hopefully not too  
dirty) way of creating the files I need. The reason I wanted to call  
all of the submethods was because I need to be able to create a random  
number of some of the individual files before I dump them into the  
array. I thought that in the main body of the code I would script it  
out to call that particular method. This is all pretty new to me…is  
that bad form?

---

<div class="post-metadata">

**Author:** ![fuglyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9dc877/32.png) [@fuglyducky](https://www.ruby-forum.com/u/fuglyducky)\
**Post date:** [May 24, 2010, 7:15pm UTC](https://www.ruby-forum.com/t/ruby-newbie-classes-and-objects-oh-my/189105/4 "2010-05-24T19:15:31Z")

</div>

On 24.05.2010 18:42, fuglyducky wrote:

> > > class XMLProcessing  
> > > try  
> > > def file\_to\_string  
> > > and here too  
> > > end  
> > > pretty nicely structured code.  
> > > The best way to predict the future is to invent it.  
> > > – Alan Kay
> 
> Wow…thanks for the response!!! I’m not a programmer for a developer  
> by any means. I’m a tester that needed a quick (and hopefully not too  
> dirty) way of creating the files I need. The reason I wanted to call  
> all of the submethods was because I need to be able to create a random  
> number of some of the individual files before I dump them into the  
> array. I thought that in the main body of the code I would script it  
> out to call that particular method. This is all pretty new to me…is  
> that bad form?

Hm, difficult where to start. If I read your code properly you have  
three file names as inputs and want to create the concatenation of the  
two first files as the third file. On shell level you could simply do

$ cat f1 f2 \> f3

There is no Ruby programming needed. Even if you want to do that in  
Ruby, there is not really a need for new classes you can do this in just  
a few lines in a function.

# Concat all files given as second, third etc. argument

# to a file with name provided as first argument

def file\_concat(out, \*in)  
File.open out, “w” do |io|  
in.each do |file\_in|  
File.foreach file\_in do |line|  
io.puts line  
end  
end  
end  
end

file\_concat(“stat.xml.txt”, “Stat.Template.xml”, “Seg.Template.xml”)

This code has the advantage over your solution with the Array that it  
easily processes arbitrarily large files because you do not have to hold  
the complete output file in memory.

A new class of your own is probably only worthwhile if you have to do  
more complex processing on the file’s content. Even if you create a  
class you should do the processing like shown in the first example, i.e.  
not hold the complete files in memory, e.g.

class XMLProcessing  
def initialize(array\_file, string\_file, output\_file)  
@array\_file = array\_file  
@string\_file = string\_file  
@output\_file = output\_file  
end

def process  
File.open @output\_file, “w” do |io|  
[@array\_file, @string\_file].each do |file\_in|  
File.foreach file\_in do |line|  
io.puts line  
end  
end  
end  
end  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![fuglyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9dc877/32.png) [@fuglyducky](https://www.ruby-forum.com/u/fuglyducky)\
**Post date:** [May 25, 2010, 9:40am UTC](https://www.ruby-forum.com/t/ruby-newbie-classes-and-objects-oh-my/189105/5 "2010-05-25T09:40:47Z")

</div>

Not at all, small methods are considered good by many, I just had the  
thought that you might call them in the initializer. As you did not  
define accessors to the instance variables I did not see any reason to  
keep the “intermediate” state between the method calls.

But again it is amazingly nice for a beginner. (I almost believe that  
you are just humble)  
Keep us updated about your progress 🙂

Cheers  
R.

---

<div class="post-metadata">

**Author:** ![fuglyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9dc877/32.png) [@fuglyducky](https://www.ruby-forum.com/u/fuglyducky)\
**Post date:** [May 26, 2010, 6:26pm UTC](https://www.ruby-forum.com/t/ruby-newbie-classes-and-objects-oh-my/189105/6 "2010-05-26T18:26:31Z")

</div>

On May 25, 12:40 am, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> R.  
> Thank you both for the information! I was able to get it working with  
> your help. I think I may need to rethink my entire script (I’ll create  
> a new post for that one because it’s beyond my understanding of Ruby  
> (or any other language)…which is minimal.
