# How to read a file and execute its code which relates to the current class

**URL:** <https://rubytalk.org/t/how-to-read-a-file-and-execute-its-code-which-relates-to-the-current-class/65253>\
**Category:** ruby-talk\
**Created:** [13 April 2012 05:44 UTC](https://rubytalk.org/t/how-to-read-a-file-and-execute-its-code-which-relates-to-the-current-class/65253 "2012-04-13T05:44:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [13 April 2012 05:44 UTC](https://rubytalk.org/t/how-to-read-a-file-and-execute-its-code-which-relates-to-the-current-class/65253/1 "2012-04-13T05:44:51Z")

</div>

I have a problem.I want to read codes from a file.And the codes in the  
file use some methods which I defined in the main process. So I don't  
know how to deal with it. Please help me.  
Thank you!

Here is an example:  
class Job  
&nbsp;&nbsp;&nbsp;def job\_handle  
&nbsp;&nbsp;&nbsp;.......  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;def job\_load  
&nbsp;&nbsp;&nbsp;File.open("the file")  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;def job\_exception  
&nbsp;&nbsp;&nbsp;end  
end  
In file "the file",the codes contain the job\_exception and job\_load  
methods.

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

---

<div class="post-metadata">

**Author:** ![Ralf\_Mueller1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ralf_mueller1/32/2123_2.png) [@Ralf\_Mueller1](https://rubytalk.org/u/Ralf_Mueller1)\
**Post date:** [13 April 2012 07:24 UTC](https://rubytalk.org/t/how-to-read-a-file-and-execute-its-code-which-relates-to-the-current-class/65253/2 "2012-04-13T07:24:59Z")

</div>

if "the file" contains ruby code, you could use "load" or "require". Search for "Including Other Files" in [Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html)

ralf

> **···**
>
> On 04/13/2012 07:44 AM, dongcan z. wrote:
> 
> > I have a problem.I want to read codes from a file.And the codes in the  
> > file use some methods which I defined in the main process. So I don't  
> > know how to deal with it. Please help me.  
> > Thank you!
> > 
> > Here is an example:  
> > class Job  
> > &nbsp;&nbsp;&nbsp;&nbsp;def job\_handle  
> > &nbsp;&nbsp;&nbsp;&nbsp;.......  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;def job\_load  
> > &nbsp;&nbsp;&nbsp;&nbsp;File.open("the file")  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;def job\_exception  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > end  
> > In file "the file",the codes contain the job\_exception and job\_load  
> > methods.

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [13 April 2012 08:17 UTC](https://rubytalk.org/t/how-to-read-a-file-and-execute-its-code-which-relates-to-the-current-class/65253/3 "2012-04-13T08:17:18Z")

</div>

Hi,

I'm not quite sure what you mean. Do you want to use the code in the  
file to define the methods (like you copied and pasted it into this  
exact position)?

If so, you can use the "instance\_eval" method:

def job\_load  
&nbsp;&nbsp;instance\_eval File.read 'code.txt'  
end

However, this isn't a very good idea.

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

---

<div class="post-metadata">

**Author:** ![Ralf\_Mueller1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ralf_mueller1/32/2123_2.png) [@Ralf\_Mueller1](https://rubytalk.org/u/Ralf_Mueller1)\
**Post date:** [13 April 2012 08:43 UTC](https://rubytalk.org/t/how-to-read-a-file-and-execute-its-code-which-relates-to-the-current-class/65253/4 "2012-04-13T08:43:50Z")

</div>

If you want to overload the Job.job\_load with an another definition from "the file" you could use ruby's mixin functionality. Right at the beginning of [Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html) you'll find what you need

> **···**
>
> On 04/13/2012 07:44 AM, dongcan z. wrote:
> 
> > I have a problem.I want to read codes from a file.And the codes in the  
> > file use some methods which I defined in the main process. So I don't  
> > know how to deal with it. Please help me.  
> > Thank you!
> > 
> > Here is an example:  
> > class Job  
> > &nbsp;&nbsp;&nbsp;&nbsp;def job\_handle  
> > &nbsp;&nbsp;&nbsp;&nbsp;.......  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;def job\_load  
> > &nbsp;&nbsp;&nbsp;&nbsp;File.open("the file")  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;def job\_exception  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > end  
> > In file "the file",the codes contain the job\_exception and job\_load  
> > methods.
