# Making separate File

**URL:** <https://www.ruby-forum.com/t/making-separate-file/140071>\
**Category:** Ruby\
**Created:** [June 10, 2008, 4:21pm UTC](https://www.ruby-forum.com/t/making-separate-file/140071 "2008-06-10T16:21:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![drweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@drweb](https://www.ruby-forum.com/u/drweb)\
**Post date:** [June 10, 2008, 4:21pm UTC](https://www.ruby-forum.com/t/making-separate-file/140071/1 "2008-06-10T16:21:56Z")

</div>

Hello,  
The given two classes (TestClass1 and TestClass2) are in a single ruby  
code. I want a code that separate both classes and save them into 2  
diffrent ruby files.

E.g

Orginal code “test.rb” (contains the classes)  
after executing

one file for 1 class “class1.rb”  
second file for second class “class2.rb”  
and so on…

## I would be very thankful to you dear

class TestClass1

```
 def method1(a,b)
    sum = a + b
    puts sum
 end

 def method2(*c)
    l = c.length
    puts l
 end

```

## end

class TestClass2

```
 def method1(a,b)
    sum = a + b
    puts sum
 end

 def method2(*c)
    l = c.length
    puts l
 end

```

end

---

<div class="post-metadata">

**Author:** ![drweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@drweb](https://www.ruby-forum.com/u/drweb)\
**Post date:** [June 10, 2008, 5:02pm UTC](https://www.ruby-forum.com/t/making-separate-file/140071/2 "2008-06-10T17:02:21Z")

</div>

2008/6/10 Michel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> second file for second class “class2.rb”  
> and so on…
> 
> I would be very thankful to you dear

For what exactly? I mean, opening two files in an editor and copy +  
pasting the code there can’t be that hard, can it?

Cheers

robert

---

<div class="post-metadata">

**Author:** ![drweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@drweb](https://www.ruby-forum.com/u/drweb)\
**Post date:** [June 10, 2008, 6:08pm UTC](https://www.ruby-forum.com/t/making-separate-file/140071/3 "2008-06-10T18:08:14Z")

</div>

Robert K. wrote:

> 2008/6/10 Michel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > second file for second class “class2.rb”  
> > and so on…
> > 
> > I would be very thankful to you dear
> 
> For what exactly? I mean, opening two files in an editor and copy +  
> pasting the code there can’t be that hard, can it?
> 
> Cheers
> 
> robert

Without opening them. It should seprate the code based on “class”  
keyword and storing it in other file using ruby code.

---

<div class="post-metadata">

**Author:** ![drweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@drweb](https://www.ruby-forum.com/u/drweb)\
**Post date:** [June 10, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/making-separate-file/140071/4 "2008-06-10T18:21:12Z")

</div>

-------- Original-Nachricht --------

> Datum: Wed, 11 Jun 2008 01:11:42 +0900  
> Von: Michel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> An: removed\_email\_address@domain.invalid  
> Betreff: Re: Making separate File

> > Cheers
> > 
> > robert
> 
> ## Without opening them. It should seprate the code based on “class” keyword and storing it in other file using ruby code.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Michel,

supposing that the original code is in “sourcefile”, and that there is  
no text in between  
the classes, you can use something like this:

text=IO.readlines(sourcefile)  
classes=text.split(/(?=class )/).delete\_if{|text|  
/^class/.match(text)==nil}  
classes.each\_with\_index{|c\_text,i|  
f=File.new(“class\_file\_” + i.to\_s + ‘.rb’,“w”)  
f.puts c\_text  
f.close  
}

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![drweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@drweb](https://www.ruby-forum.com/u/drweb)\
**Post date:** [June 10, 2008, 7:24pm UTC](https://www.ruby-forum.com/t/making-separate-file/140071/5 "2008-06-10T19:24:58Z")

</div>

This error is arises when i run the code:  
“private method `split’ called for #Array:0x28402b8 (NoMethodError)”

Actually i am new to ruby thats why i have

---

<div class="post-metadata">

**Author:** ![drweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@drweb](https://www.ruby-forum.com/u/drweb)\
**Post date:** [June 10, 2008, 7:37pm UTC](https://www.ruby-forum.com/t/making-separate-file/140071/6 "2008-06-10T19:37:45Z")

</div>

-------- Original-Nachricht --------

> Datum: Wed, 11 Jun 2008 02:24:45 +0900  
> Von: Michel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> An: removed\_email\_address@domain.invalid  
> Betreff: Re: Making separate File

> This error is arises when i run the code:  
> “private method `split’ called for #Array:0x28402b8 (NoMethodError)”
> 
> Actually i am new to ruby thats why i have
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Michel,

sorry, that was my fault.  
You must change the line with IO.readlines to

text=IO.readlines(sourcefile).to\_s

Otherwise, text is an Array, but with to\_s, it is converted to a String.  
And that’s what the Regular Expression needs.

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![drweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@drweb](https://www.ruby-forum.com/u/drweb)\
**Post date:** [June 10, 2008, 8:11pm UTC](https://www.ruby-forum.com/t/making-separate-file/140071/7 "2008-06-10T20:11:22Z")

</div>

On 10.06.2008 19:37, Axel E. wrote:

> -------- Original-Nachricht --------
> 
> > Datum: Wed, 11 Jun 2008 02:24:45 +0900  
> > Von: Michel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > An: removed\_email\_address@domain.invalid  
> > Betreff: Re: Making separate File
> 
> > This error is arises when i run the code:  
> > “private method `split’ called for #Array:0x28402b8 (NoMethodError)”
> > 
> > Actually i am new to ruby thats why i have

> sorry, that was my fault.  
> You must change the line with IO.readlines to
> 
> text=IO.readlines(sourcefile).to\_s

Or just do

test = IO.read(sourcefile)

Cheers

robert
