# Help with Class definition and calling a Ruby Program

**URL:** <https://rubytalk.org/t/help-with-class-definition-and-calling-a-ruby-program/57807>\
**Category:** ruby-talk\
**Created:** [19 March 2010 18:52 UTC](https://rubytalk.org/t/help-with-class-definition-and-calling-a-ruby-program/57807 "2010-03-19T18:52:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Reinhard\_Lange](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@Reinhard\_Lange](https://rubytalk.org/u/Reinhard_Lange)\
**Post date:** [19 March 2010 18:52 UTC](https://rubytalk.org/t/help-with-class-definition-and-calling-a-ruby-program/57807/1 "2010-03-19T18:52:38Z")

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

&nbsp;&nbsp;def SplitCode(sCodeFile,sListFile)  
&nbsp;&nbsp;# param1: file to extract code from  
&nbsp;&nbsp;# param2: file list to verify with  
&nbsp;&nbsp;# read param1. If &&&&& is found, close OUT file if open.  
&nbsp;&nbsp;&nbsp;&nbsp;fReadCodeFile = File.new(sCodeFile,'r')  
&nbsp;&nbsp;&nbsp;&nbsp;while (sLine = fReadCodeFile.gets)

> **···**
>
> #  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (sLine == "&&&&&")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fWriteCodeFile.close if !(fWriteCodeFile.closed?)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fWriteCodeFile = File.new(fReadCodeFile.gets,'w')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end # if sLine == &&&& ...  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (sLine != "=====")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fWriteCodeFile.puts sLine  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end # if sLine != ====  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;end # while...  
> &nbsp;&nbsp;&nbsp;fWriteCodeFile.close if !(fWriteCodeFile.closed?)  
> &nbsp;&nbsp;&nbsp;fReadCodeFile.close if !(fReadCodeFile.closed?)  
> &nbsp;&nbsp;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.  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Aldric\_Giacomoni2](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@Aldric\_Giacomoni2](https://rubytalk.org/u/Aldric_Giacomoni2)\
**Post date:** [19 March 2010 19:45 UTC](https://rubytalk.org/t/help-with-class-definition-and-calling-a-ruby-program/57807/2 "2010-03-19T19:45:05Z")

</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:-
> 
> &nbsp;&nbsp;def SplitCode(sCodeFile,sListFile)

[.............]

> &nbsp;&nbsp;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  
&nbsp;&nbsp;File.readlines(s\_code\_file).each do |line|  
&nbsp;&nbsp;&nbsp;&nbsp;case line  
&nbsp;&nbsp;&nbsp;&nbsp;when "&&&&&"

&nbsp;&nbsp;&nbsp;&nbsp;when "====="

&nbsp;&nbsp;&nbsp;&nbsp;end # end case  
&nbsp;&nbsp;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.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Siep\_Korteling](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Siep\_Korteling](https://rubytalk.org/u/Siep_Korteling)\
**Post date:** [19 March 2010 20:57 UTC](https://rubytalk.org/t/help-with-class-definition-and-calling-a-ruby-program/57807/3 "2010-03-19T20:57:11Z")

</div>

Reinhard Lange wrote:

> &nbsp;&nbsp;def SplitCode(sCodeFile,sListFile)  
> &nbsp;&nbsp;(...)  
> 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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [20 March 2010 22:17 UTC](https://rubytalk.org/t/help-with-class-definition-and-calling-a-ruby-program/57807/4 "2010-03-20T22:17:21Z")

</div>

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.

> **···**
>
> On Fri, Mar 19, 2010 at 9:57 PM, Siep Korteling \<s.korteling@gmail.com\> wrote:
> 
> > 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")
