Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Processing code file inline
Greg Willits
8/6/2008 7:37:00 AM
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.
Example:
# file to load
index_33dob.store("NatVir20000515".to_sym, 67)
index_33dob.store("MicSwa19920210".to_sym, 85)
index_33dob.store("AshSmi19950827".to_sym, 19)
index_33dob.store("IvaCov20021108".to_sym, 45)
index_33dob.store("CatOrt19950128".to_sym, 83)
# contrived script to load the file and process it
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).
thx
-- gw
--
Posted via
http://www.ruby-...
.
2 Answers
Greg Willits
8/6/2008 7:43:00 AM
0
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 Willits 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.
>
> Example:
>
> # file to load
> index_33dob.store("NatVir20000515".to_sym, 67)
> index_33dob.store("MicSwa19920210".to_sym, 85)
> index_33dob.store("AshSmi19950827".to_sym, 19)
> index_33dob.store("IvaCov20021108".to_sym, 45)
> index_33dob.store("CatOrt19950128".to_sym, 83)
>
> # contrived script to load the file and process it
> 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).
>
> thx
>
> -- gw
--
Posted via
http://www.ruby-...
.
Farrel Lifson
8/6/2008 7:46:00 AM
0
2008/8/6 Greg Willits <lists@gregwillits.ws>:
>
> # contrived script to load the file and process it
> 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
--
Aimred - Ruby Development and Consulting
http://www....
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Processing code file inline
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password