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

Discussion in 'Ruby' started by eric.promislow@gmail.com, Jul 30, 2005.

  1. Guest

    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
     
    , Jul 30, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. yogesh
    Replies:
    3
    Views:
    610
    Kenny McCormack
    Feb 12, 2006
  2. Daniel Schierbeck

    Kernel#require accepting multiple arguments?

    Daniel Schierbeck, Oct 30, 2005, in forum: Ruby
    Replies:
    4
    Views:
    119
    Eric Hodel
    Oct 31, 2005
  3. Replies:
    3
    Views:
    187
  4. Redd Vinylene
    Replies:
    6
    Views:
    335
    Jakub Pawlowicz
    Nov 18, 2008
  5. Lars Gierth
    Replies:
    6
    Views:
    244
    David Masover
    Mar 20, 2010
Loading...

Share This Page