Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ruby Newbie...Classes and Objects, oh my!

Reply
Thread Tools

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

 
 
fuglyducky
Guest
Posts: n/a
 
      05-24-2010
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")

################################################## #############
 
Reply With Quote
 
 
 
 
Robert Dober
Guest
Posts: n/a
 
      05-24-2010
On Mon, May 24, 2010 at 4:30 PM, fuglyducky <> wrote:
> 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
> =A0 =A0 =A0 =A0def initialize(array_file, string_file, output_file)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0@array_file =3D array_file
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0@string_file =3D string_file
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0@output_file =3D output_file
> =A0 =A0 =A0 =A0end
>
> =A0 =A0 =A0 =A0def lines_to_array
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# Dump each line from text file into array
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0File.open(@array_file).each do |line|
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0my_array << line

This is a local variable, uninitialized for that matter
try
@my_array =3D []
File.open...
@my_array << line
end

However, you could also simply do a
@my_array =3D File.readlines( @array_file ).map( &:chomp )
or the more explicit (and in Rubies < 1.9 necessary )
@my_array =3D File.readlines( @array_file ).map{ |line| line.chomp }
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0end
> =A0 =A0 =A0 =A0end
>
> =A0 =A0 =A0 =A0def file_to_string
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# Create string from text file
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0@my_string_file =3D File.open(@string_file=

)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0my_string =3D @my_string_file.read

Same here
@my_string =3D ...

> =A0 =A0 =A0 =A0end
>
> =A0 =A0 =A0 =A0def string_into_array
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# Insert string into array
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0my_array.insert((my_array.length - 1), my_=

string)
and here too
@my_array
> =A0 =A0 =A0 =A0end
>
> =A0 =A0 =A0 =A0def new_array_to_file
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# Dump the array to a text file
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0newFile =3D File.new(@output_file, "w+")
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0my_array.each { |element| newFile.puts ele=

ment }
change here too
@my_array
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0print 'New file created: ' + (@output_file=

)
> =A0 =A0 =A0 =A0end
> end
>
>
> stat_xml =3D 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.
>
> ################################################## #############
>
>




--=20
The best way to predict the future is to invent it.
-- Alan Kay

 
Reply With Quote
 
 
 
 
fuglyducky
Guest
Posts: n/a
 
      05-24-2010
On May 24, 8:57*am, Robert Dober <robert.do...@gmail.com> wrote:
> On Mon, May 24, 2010 at 4:30 PM, fuglyducky <fuglydu...@gmail.com> wrote:
> > 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

>
> 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.
>
>
>
> > ################################################## #############

>
> --
> 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?
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      05-24-2010
On 24.05.2010 18:42, fuglyducky wrote:
> On May 24, 8:57 am, Robert Dober <robert.do...@gmail.com> wrote:
>> On Mon, May 24, 2010 at 4:30 PM, fuglyducky <fuglydu...@gmail.com> wrote:
>>> 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

>> 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.
>>
>>
>>
>>> ################################################## #############

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      05-25-2010
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ruby extension (C++) on OS X [ruby 1.8.2] and Google-Sketchup [ruby 1.8.5] Nicholas Ruby 3 01-28-2007 01:48 AM
The Ruby Edge - Digg for Ruby and Ruby On Rails roschler Ruby 0 10-15-2006 11:35 PM
ruby-talk, comp.lang.ruby, ruby-talk-google Phrogz Ruby 4 09-05-2006 08:19 PM
#!/usr/bin/ruby , #!/usr/bin/ruby -w , #!/usr/bin/ruby -T?, #!/usr/bin/ruby -T1... anne001 Ruby 1 04-23-2006 03:02 PM
[ANN] ruby-freedb, ruby-serialport, ruby-mp3info moved to Rubyforge guillaume.pierronnet@ratp.fr Ruby 0 08-31-2003 11:57 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57