StringIO (was: Namespaces (was: protected program in a program))

Check this out!

def executeCode (code, input) # I’m just ignoring input for now.
code = “begin\n” +
code +
"\nrescue Exception => error\n" +
“puts error.inspect\n” +
"end\n"
strIO = StringIO.new
strIO.instance_eval code
strIO.string
end

I don’t fully understand this code… The StringIO parts. Half of it is
from the ML, and half is me trying to figure out what StringIO is (god bless
irb!!!).

Well, it works great. No namespace issues, no redirecting, and it even
catches errors!

So, now I just need some way to feed input into StringIO, I guess… so that
if did this:

code = <<-END_CODE
str1 = gets
str2 = gets
puts str1
puts str2
END_CODE

input = “hello\n” + “hi\n”

output = executeCode (code, input)

…I’d get back “hello\nhi\n”.

Any ideas?

Chris

Hi,

def executeCode (code, input) # I’m just ignoring input for now.
code = “begin\n” +
code +
“\nrescue Exception => error\n” +
“puts error.inspect\n” +
“end\n”
strIO = StringIO.new
if input
input = StringIO.new(input, “r”)
class << strIO; self; end.class_eval do
%w[gets getc read].each do |f|
define_method(f, &input.method(f))
end
end
end

···

At Sat, 7 Dec 2002 10:11:51 +0900, Chris Pine wrote:

strIO.instance_eval code
strIO.string

end


Nobu Nakada

Oh, it’s beautiful! Thank you, thank you, thank you!

I never would have figured that out.

Where can I learn more about the StringIO class? If it’s not in my beloved
pickaxe, I don’t know about it. :frowning: Do one of the other books cover this?

Now I just need to figure out how to grab the input as it’s being grabbed
so I can echo it to the output…

Chris

…replying to my own post…

···

----- Original Message -----
Now I just need to figure out how to grab the input as it’s being grabbed
so I can echo it to the output…

Nevermind. It was pretty easy:

def executeCode (code, input)
code = “begin\n” +
code +
"\nrescue Exception => error\n" +
“puts error.inspect\n” +
“end\n”

strIO = StringIO.new
if !input.empty?
  input = input.join("\n")+"\n"
  input = StringIO.new (input, "r")
  class << strIO; self; end.class_eval do
    %w[gets getc read].each do |f|
      define_method(f) do
        inStr = input.method(f).call
        puts '<'+inStr+'>'
        inStr
      end
    end
  end
end
strIO.instance_eval code
strIO.string

end

It’s just that I don’t know anything about define_method or StringIO, so I
feel lost… also, I didn’t know you could associate Method objects to a
method call just like Proc objects… could you always do that? It makes
sense…

Chris

Hi,

Where can I learn more about the StringIO class?

The source :wink:

Its usage should be almost same as IO, except for diferrences
written in ext/stringio/README file.

If it’s not in my beloved pickaxe, I don’t know about it. :frowning:
Do one of the other books cover this?

StringIO was added after the PickAxe, so it’s not of course. I
don’t know about (English) books well.

Now I just need to figure out how to grab the input as it’s being grabbed
so I can echo it to the output…

Sorry, but I don’t know what you mean by “as grabbed”.

···

At Sat, 7 Dec 2002 12:52:54 +0900, Chris Pine wrote:


Nobu Nakada

Hi,

···

At Sat, 7 Dec 2002 13:05:53 +0900, Chris Pine wrote:

It’s just that I don’t know anything about define_method or StringIO, so I
feel lost… also, I didn’t know you could associate Method objects to a
method call just like Proc objects… could you always do that? It makes
sense…

& before input.method(f) means the Method object should be
converted to block. Or you can:

define_method(f, input.method(f).to_proc)


Nobu Nakada