Help with Class definition and calling a Ruby Program

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.

Reinhard Lange wrote:

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.

Reinhard Lange wrote:

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

On Fri, Mar 19, 2010 at 9:57 PM, Siep K. [email protected]
wrote:

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

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.