Better way to extend Kernel#eval -- can't require 'openssl'

E

eric.promislow

This program doesn't work
(ruby 1.8.2 (2004-12-25) [i386-mswin32]):

# ==== 8-< =====
module Kernel
alias_method :eek:ld_eval, :eval
def eval(*args)
res = old_eval(*args)
print "**** eval(#{args}) => #{res}\n"
return res
end
end

require 'openssl'

print "Done\n"
# ==== 8-< =====

If I run it I get this output:

eval_wrapper.rb:4:in `eval': (eval):1:in `eval': uninitialized constant
Kernel::
Cipher (NameError)
from c:/ruby/lib/ruby/site_ruby/1.8/openssl/cipher.rb:24:in
`old_eval'
from eval_wrapper.rb:4:in `eval'
from c:/ruby/lib/ruby/site_ruby/1.8/openssl/cipher.rb:24
from c:/ruby/lib/ruby/site_ruby/1.8/openssl/cipher.rb:23:in
`each'
from c:/ruby/lib/ruby/site_ruby/1.8/openssl/cipher.rb:23
from c:/ruby/lib/ruby/site_ruby/1.8/openssl.rb:20:in `require'
from c:/ruby/lib/ruby/site_ruby/1.8/openssl.rb:20
from eval_wrapper.rb:10:in `require'
from eval_wrapper.rb:10

Here's the problematic code from openssl:

module OpenSSL
module Cipher
%w(AES Cast5 BF DES Idea RC2 RC4 RC5).each{|cipher|
eval(<<-EOD)
class #{cipher} < OpenSSL::Cipher::Cipher
def initialize(*args)
args = args.join('-')
if args.size == 0
super(\"#{cipher}\")
else
super(\"#{cipher}-#\{args\}\")
end
end
end
EOD
}

If I change the first line in the eval block from "... < Cipher"
to "< OpenSSL::Cipher::Cipher" it works.

The problem is obviously that the hook causes the eval block to be
eval'ed in the hook's binding, not the original binding. What's
the best way to get the binding I need? Yes, I have a trace-function,
but I don't see an obvious event that tells me the eval hook is
about to be called.

Thanks,
Eric
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top