# Help with Class definition and calling a Ruby Program

**URL:** <https://www.ruby-forum.com/t/help-with-class-definition-and-calling-a-ruby-program/185432>\
**Category:** Ruby\
**Created:** [March 19, 2010, 7:52pm UTC](https://www.ruby-forum.com/t/help-with-class-definition-and-calling-a-ruby-program/185432 "2010-03-19T19:52:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![raperswil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6a8cbe/32.png) [@raperswil](https://www.ruby-forum.com/u/raperswil)\
**Post date:** [March 19, 2010, 7:52pm UTC](https://www.ruby-forum.com/t/help-with-class-definition-and-calling-a-ruby-program/185432/1 "2010-03-19T19:52:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![raperswil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6a8cbe/32.png) [@raperswil](https://www.ruby-forum.com/u/raperswil)\
**Post date:** [March 19, 2010, 8:45pm UTC](https://www.ruby-forum.com/t/help-with-class-definition-and-calling-a-ruby-program/185432/2 "2010-03-19T20:45:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![raperswil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6a8cbe/32.png) [@raperswil](https://www.ruby-forum.com/u/raperswil)\
**Post date:** [March 19, 2010, 9:57pm UTC](https://www.ruby-forum.com/t/help-with-class-definition-and-calling-a-ruby-program/185432/3 "2010-03-19T21:57:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![raperswil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6a8cbe/32.png) [@raperswil](https://www.ruby-forum.com/u/raperswil)\
**Post date:** [March 20, 2010, 11:18pm UTC](https://www.ruby-forum.com/t/help-with-class-definition-and-calling-a-ruby-program/185432/4 "2010-03-20T23:18:02Z")

</div>

On Fri, Mar 19, 2010 at 9:57 PM, Siep K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.
