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

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
   def job_handle
   .......
   end
   def job_load
   File.open("the file")
   end
   def job_exception
   end
end
In file "the file",the codes contain the job_exception and job_load
methods.

···

--
Posted via http://www.ruby-forum.com/.

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

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
    def job_handle
    .......
    end
    def job_load
    File.open("the file")
    end
    def job_exception
    end
end
In file "the file",the codes contain the job_exception and job_load
methods.

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
  instance_eval File.read 'code.txt'
end

However, this isn't a very good idea.

···

--
Posted via http://www.ruby-forum.com/.

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 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
    def job_handle
    .......
    end
    def job_load
    File.open("the file")
    end
    def job_exception
    end
end
In file "the file",the codes contain the job_exception and job_load
methods.