Here’s what I’ve got (and it ain’t a lot)…
I guess, as far as namespaces go, I’m looking for something like Kernel.load
with the wrap parameter set to ‘true’. Can I do something like this, but
with the code in a string, rather than in a file?
(I’m trying to display code and its output in a mod_ruby generated webpage.)
I could just make up a new random module name for each code sample, each
time the page is read. However, won’t this create thousands and thousands
of modules eventually? Are these modules ever cleaned up? Or is it OK,
since mod_ruby is loading my script with ‘wrap = true’?
···
----- Original Message -----
From: “Chris Pine” nemo@hellotree.com
Hello,
I’ve got a kind of tricky question. For my tutorial there are lots of code
samples, and I usually want to display the output of them in the page. I’d
like to automate this. I’m generating all of the pages in mod_ruby.
Because there are so many of them, though, I’d like them to be safe from
each other… I guess each one should be in it’s own namespace? (I don’t
know much about namespaces… better not cover that in the tutorial!)
I’m hoping for something like this:
code = "puts ‘hello’"
output = executeCode (code)
output # should be “hello\n”
Each code sample is supposed to be it’s own program, so I don’t want any
possibilities of variables showing up in code samples further on down the
page, and certainly not on subsequent calls to that page! Does any one
know how I could do this?
OK, if you can handle that, how about this…
Many of the programs require input. Is there a way I could queue up the
input I want to send the program before it is run? Something like this:
code = <<-'END_CODE
str1 = gets
str2 = gets
puts str1
puts str2
END_CODE
input = “hello\n”+
“hi\n”
Then I’ll strip the leading whitespace out of the code.
output = executeCode (code, input)
If you can do that, you have helped me greatly. However, the perfect
solution would be a little bit trickier…
I would like to echo the input back into the output in the right place (just
like would happen when running such a program from the commandline), and
surround it in special tags so I can change it’s style from the
’output’ style to the ‘input’ style.
Do I have a hero out there?
Chris
EXTRA CREDIT: Point me to some regexps for Ruby syntax coloring! I’m sure
I don’t need to reinvent that wheel.