I have a bit of code here that I’d love to make better:
I don’t want to continue down the path of reading an entire file into a
string and then eval’ing that string just to get the contents of the
file executed as if it were the body of a method in a class. The file
passed to the constructor above might contain something like:
@foo = ‘This will become an instance variable when you do Foo.new(path)’
I’ve tried all manner of hacks to make Kernel#load and friends execute
code as if it were in the calling scope. In every case I’ve seen the
loaded file leave no lasting effect on the instance variables of the
calling method. My goal is to be able to do exactly this:
f = Foo.new(‘load_me.rb’)
f.instance_variables # => [’@foo’]
My assumption here is that reading a file into a string and eval’ing it
is very slow and is bad form. Am I right?
Is this possible without reading the file into a string? Is this
possible without hacking the C code? Should I go bother ruby-core?