I need to have a script load a file and execute that file as if it was
code inline to the script. The file being loaded is not a class, not a
module. It is code that has been dynamically written by another process.
timer_start = Time.new
index_33dob = {}
include “33dob_entries.rb” # <<— WHAT DO HERE ?
timer_stop = Time.new
puts “#{timer_stop - timer_start}”
p index_33dob
In other languages I have used, the command include would be used to
integrate the 33dob_entries.rb file as inline code, but neither Ruby’s
include nor require can be used to do this AFAICT.
How would I do this with Ruby ?
(I am writing some data structures to disk as code instead of using
Marshal because it is actually much much faster than reconstituing the
data via Marshal, and that re-loading performance is critical to the
app).
Dang, thought of one more thing right after I sent this and it worked.
I needed to use @index_33dob instead of index_33dob in order to have the
var from the loaded file available to the script.
– gw
Greg W. wrote:
I need to have a script load a file and execute that file as if it was
code inline to the script. The file being loaded is not a class, not a
module. It is code that has been dynamically written by another process.
timer_start = Time.new
index_33dob = {}
include “33dob_entries.rb” # <<— WHAT DO HERE ?
timer_stop = Time.new
puts “#{timer_stop - timer_start}”
p index_33dob
In other languages I have used, the command include would be used to
integrate the 33dob_entries.rb file as inline code, but neither Ruby’s
include nor require can be used to do this AFAICT.
How would I do this with Ruby ?
(I am writing some data structures to disk as code instead of using
Marshal because it is actually much much faster than reconstituing the
data via Marshal, and that re-loading performance is critical to the
app).
timer_start = Time.new
index_33dob = {}
include “33dob_entries.rb” # <<— WHAT DO HERE ?
timer_stop = Time.new
puts “#{timer_stop - timer_start}”
p index_33dob
Read the file into a string, and then use Kernel#eval. Just a warning
that this can be very unsafe if you don’t trust the processes
generating the code.
Regards,
Farrel
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.