Wrapped load

P

Patrick Hurley

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
 
A

Ara.T.Howard

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
--
===============================================================================
| 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
===============================================================================
 
J

Joel VanderWerf

Patrick said:
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...?
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top