Help with Class definition and calling a Ruby Program

R

Reinhard Lange

All
I am new to Ruby and to OO programming.

I need to get the following done which I have written in a function:-

def SplitCode(sCodeFile,sListFile)
# param1: file to extract code from
# param2: file list to verify with
# read param1. If &&&&& is found, close OUT file if open.
fReadCodeFile = File.new(sCodeFile,'r')
while (sLine = fReadCodeFile.gets)
#
if (sLine == "&&&&&")
fWriteCodeFile.close if !(fWriteCodeFile.closed?)
fWriteCodeFile = File.new(fReadCodeFile.gets,'w')
end # if sLine == &&&& ...
#
if (sLine != "=====")
fWriteCodeFile.puts sLine
end # if sLine != ====
#
end # while...
fWriteCodeFile.close if !(fWriteCodeFile.closed?)
fReadCodeFile.close if !(fReadCodeFile.closed?)
end #end of function

What I need is to be able to pass the arguments to the function and call
it.
Do I need to:
/mycode.rb file1.txt file2.txt

Thanks in advance for any help.
 
A

Aldric Giacomoni

Reinhard said:
All
I am new to Ruby and to OO programming.

I need to get the following done which I have written in a function:-

def SplitCode(sCodeFile,sListFile) [ .............]
end #end of function

What I need is to be able to pass the arguments to the function and call
it.
Do I need to:
./mycode.rb file1.txt file2.txt

We don't know enough to really help you. It's a method that's stuck in
the middle of other code, and it's not the Ruby way to do things,
either. My guess is .. C#, or something like that.
Try this code skeleton:

def split_code s_code_file, s_list_file
File.readlines(s_code_file).each do |line|
case line
when "&&&&&"

when "====="

end # end case
end # end readlines
end # end method

It's a little cleaner. If you don't like the "case" in the middle, then
stick to your if/else, which is fine ..

If you want to pass arguments to a method from the command line, then
those arguments translate inside the Ruby program as an array of strings
called ARGV, and you have to do something with those.
If you're confused, then it means you're still sane. Give us a little
more info.
 
S

Siep Korteling

Reinhard said:
def SplitCode(sCodeFile,sListFile)
(...)
What I need is to be able to pass the arguments to the function and call
it.
Do I need to:
./mycode.rb file1.txt file2.txt

Thanks in advance for any help.

Don't use a capitalized method name (see Aldric's suggestion); only
classes and constants are capitalized.
Passing arguments to the method is the same as in most languages:

split_code("file1.txt", "file2.txt")

hth,

Siep
 
J

Jesús Gabriel y Galán

Don't use a capitalized method name (see Aldric's suggestion); only
classes and constants are capitalized.
Passing arguments to the method is the same as in most languages:

split_code("file1.txt", "file2.txt")

Putting together the previous two suggestions, if you call your script
as ./mycode.rb file1.txt file2.txt, then:

split_code(ARGV[0], ARGV[1])

Jesus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top