eval and dynamic local variable creation in Ruby 1.8 vs 1.9

Discussion in 'Ruby' started by Thairuby Thairuby, Jun 8, 2008.

  1. I tested this script with "Try Ruby! (in your browser)"
    (http://tryruby.hobix.com/).

    >> RUBY_VERSION

    => "1.8"
    >> eval "a=1"

    => 1
    >> a

    => 1

    It is the same as my expectation. But when I try with Ruby 1.9 in my
    computer...

    irb(main):001:0> RUBY_VERSION
    => "1.9.0"
    irb(main):002:0> eval "a=1"
    => 1
    irb(main):003:0> a
    NameError: undefined local variable or method `a' for main:Object
    ...


    I read from somewhere that now Ruby can't dynamically create local
    variable. Is it true or just a bug?
    Sorry with my poor english.
    --
    Posted via http://www.ruby-forum.com/.
    Thairuby Thairuby, Jun 8, 2008
    #1
    1. Advertising

  2. Thairuby Thairuby wrote:
    > I read from somewhere that now Ruby can't dynamically create local
    > variable. Is it true or just a bug?
    > Sorry with my poor english.


    In Ruby 1.8, all evals under a given scope (like, at the same scoped
    level in a method) used the same shared local variable scope. That scope
    grew as needed to accommodate new variables.

    In Ruby 1.9, every eval gets its own scope. This provides better
    isolation between evals, which has both positive and negative side
    effects. On the positive side, the code inside an eval can use a faster
    representation of local variables that doesn't depend on being able to
    grow. On the negative side, you can't do things like this anymore:

    eval "a = 1"
    eval "puts a"

    - Charlie
    Charles Oliver Nutter, Jun 8, 2008
    #2
    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. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    542
    Karl Seguin [MVP]
    Jun 16, 2006
  2. Massi
    Replies:
    26
    Views:
    478
    88888 Dihedral
    Dec 13, 2011
  3. John Lane
    Replies:
    8
    Views:
    132
    Rick DeNatale
    Apr 16, 2010
  4. Mel
    Replies:
    5
    Views:
    148
    kaeli
    Aug 20, 2004
  5. Liang Wang
    Replies:
    8
    Views:
    129
    Ben Morrow
    Feb 2, 2008
Loading...

Share This Page