Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Better way to extend Kernel#eval -- can't require 'openssl'

Reply
Thread Tools

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

 
 
eric.promislow@gmail.com
Guest
Posts: n/a
 
      07-30-2005
This program doesn't work
(ruby 1.8.2 (2004-12-25) [i386-mswin32]):

# ==== 8-< =====
module Kernel
alias_method 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a.extend(b) better than a+=b ? candide Python 6 04-22-2010 10:20 AM
Is it better to extend a class, or do something repetitious in themain part of a program? J Python 1 04-21-2010 08:34 AM
Which way is the best way to require another file in .rb Eddy Xu Ruby 5 04-11-2008 06:25 PM
Dear RubyGems: Perhaps a better way to override require... Trans Ruby 3 08-11-2005 08:48 PM
Build a Better Blair (like Build a Better Bush, only better) Kenny Computer Support 0 05-06-2005 04:50 AM



Advertisments