Wrapped load

I am building an internal batch processor -- I am not worried about
malicious code (rm -rf *) type stuff, but I would like to be able to
run most any ruby code without worrying about side effects in the
batch processor, so a wrapped load sounded perfect.

I wanted to isolate changes to existing classes, so I put together
this ever useful program (foo.rb):

module Kernel
  alias old_puts puts

  def puts(*args)
    args.each do |stuff|
      old_puts "Fancy #{stuff}"
    end
  end

  def boo(x)
    puts x ** 2
  end

end

puts "Wow"

------------------------------ and then this even more impressive test
body ------
puts "One"
load('foo.rb', true)
puts "Two"
load('foo.rb', false)
puts "Three"

···

-------------------------------------------------------------------------------------------------------
Which outputs:

One
Wow
Two
Fancy Wow
Fancy Three

What I really wanted was:
One
Fancy Wow (how do I get this?)
Two
Fancy Wow
Fancy Three

As it stands the wrapped code does not execute the same -- is there a
way to get it to, without having those effects leak into the loading
application?

Thanks
Patrick

you may find this useful

   http://raa.ruby-lang.org/search.rhtml?search=dynaload

i use it for something quite simlar.

cheers.

-a

···

On Mon, 20 Jun 2005, Patrick Hurley wrote:

I am building an internal batch processor -- I am not worried about
malicious code (rm -rf *) type stuff, but I would like to be able to
run most any ruby code without worrying about side effects in the
batch processor, so a wrapped load sounded perfect.

I wanted to isolate changes to existing classes, so I put together
this ever useful program (foo.rb):

module Kernel
alias old_puts puts

def puts(*args)
   args.each do |stuff|
     old_puts "Fancy #{stuff}"
   end
end

def boo(x)
   puts x ** 2
end

end

puts "Wow"

------------------------------ and then this even more impressive test
body ------
puts "One"
load('foo.rb', true)
puts "Two"
load('foo.rb', false)
puts "Three"

-------------------------------------------------------------------------------------------------------
Which outputs:

One
Wow
Two
Fancy Wow
Fancy Three

What I really wanted was:
One
Fancy Wow (how do I get this?)
Two
Fancy Wow
Fancy Three

As it stands the wrapped code does not execute the same -- is there a
way to get it to, without having those effects leak into the loading
application?

Thanks
Patrick

--

email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
My religion is very simple. My religion is kindness.
--Tenzin Gyatso

===============================================================================

Patrick Hurley wrote:

I am building an internal batch processor -- I am not worried about
malicious code (rm -rf *) type stuff, but I would like to be able to
run most any ruby code without worrying about side effects in the
batch processor, so a wrapped load sounded perfect.

I wanted to isolate changes to existing classes, so I put together
this ever useful program (foo.rb):

module Kernel
  alias old_puts puts

  def puts(*args)
    args.each do |stuff|
      old_puts "Fancy #{stuff}"
    end
  end

  def boo(x)
    puts x ** 2
  end

end

puts "Wow"

------------------------------ and then this even more impressive test
body ------
puts "One"
load('foo.rb', true)
puts "Two"
load('foo.rb', false)
puts "Three"

-------------------------------------------------------------------------------------------------------
Which outputs:

One
Wow
Two
Fancy Wow
Fancy Three

What I really wanted was:
One
Fancy Wow (how do I get this?)
Two
Fancy Wow
Fancy Three

As it stands the wrapped code does not execute the same -- is there a
way to get it to, without having those effects leak into the loading
application?

Thanks
Patrick

I think this goes beyond just wrapping the file in a module, as you found out. It sounds like you want every object created by the loaded file to have its #puts method replaced with the one you defined. That's starting to sound like David Alan Black's Behaviors concept:

http://www.wobblini.net/ruby/behaviors/

Maybe David can comment...?