[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Wrapped load

Patrick Hurley

6/19/2005 5:26:00 PM

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


2 Answers

Ara.T.Howard

6/19/2005 9:44:00 PM

0

Joel VanderWerf

6/20/2005 5:56:00 AM

0

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/...

Maybe David can comment...?