Passing in CGI parameters to an .rhtml file with StringIO#instance_eval

F

Francis Hwang

There's probably something really simple I'm missing here, but: I'm
trying to run an Eruby file from inside a Ruby class, and find a
general way to pass in the CGI parameters, but I'm stumbling on that.
I figured that for a get, CGI parameters are effectively passed in via
$stdin, so I tried this:

compiler = ERuby::Compiler.new()
src = nil
File.open( @rhtml ) { |file| src = compiler.compile_file( file ) }
arg_strings = args.collect { |pair|
"#{ pair.first.to_s }=#{ pair.last.to_s }"
}
arg_str = arg_strings.join( '&' )
stringio_in = StringIO.new( arg_str )
orig_stdin = $stdin
$stdin = StringIO.new( arg_str )
begin
stringio = StringIO.new
stringio.instance_eval src
stringio.string
ensure
$stdin = orig_stdin
end

But that doesn't seem to work. Suggestions?

Francis
 
F

Francis Hwang

James Britt said:
Francis said:
But that doesn't seem to work. Suggestions?

Can you just set environment variables?

ENV['QUERY_STRING'] = 'foo=bar&x=2&this=that'


James

Actually, I spoke too soon. The code I posted above does seem to work,
I was just misreading the error I was getting:

../integrate.rb:27:in `instance_eval'(TestSendNetArtNewsStory):
(eval):11:in `instance_eval'undefined method `include' for
#<StringIO:0xbf2402d8> (NoMethodError)

that's because on line 11 of the .rhtml file, there's an "include
Lafcadio" statement, which I guess doesn't work if it's called in the
context of a StringIO. (I tried Module.include as well, but that
complains that it's a private method.) Any ideas?

Setting the environment variable didn't work, btw.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top