Module Eval Syntax

Discussion in 'Ruby' started by John W. Long, Jan 26, 2006.

  1. John W. Long

    John W. Long Guest

    >> class MyModule; end
    => nil
    >> MyModule.class_variables

    => []
    >> MyModule.module_eval do

    ?> @@test = true
    >> end

    => true
    >> @@test

    => true
    >> MyModule.module_eval "@@weird = true"

    => true
    >> @@weird

    NameError: uninitialized class variable @@weird in Object
    from (irb):8
    >> MyModule.class_variables

    => ["@@weird", "@@test"]
    >> Object.class_variables

    => ["@@test"]

    What in the world is going on here? Why does the block syntax eval
    differently than the string syntax? And why does @@test get defined on
    both MyModule and Object?

    --
    John Long
    http://wiseheartdesign.com
    John W. Long, Jan 26, 2006
    #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. Eric Newton
    Replies:
    3
    Views:
    9,331
    Brock Allen
    Apr 4, 2005
  2. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    513
    Karl Seguin [MVP]
    Jun 16, 2006
  3. Alex van der Spek

    eval('07') works, eval('08') fails, why?

    Alex van der Spek, Jan 8, 2009, in forum: Python
    Replies:
    6
    Views:
    1,381
    Bruno Desthuilliers
    Jan 8, 2009
  4. Liang Wang
    Replies:
    8
    Views:
    116
    Ben Morrow
    Feb 2, 2008
  5. Marc Girod

    to eval or not to eval?

    Marc Girod, Apr 19, 2011, in forum: Perl Misc
    Replies:
    2
    Views:
    141
    Marc Girod
    Apr 19, 2011
Loading...

Share This Page