# Eval and dynamic local variable creation in Ruby 1.8 vs 1.9

**URL:** <https://www.ruby-forum.com/t/eval-and-dynamic-local-variable-creation-in-ruby-1-8-vs-1-9/139866>\
**Category:** Ruby\
**Created:** [June 8, 2008, 12:57pm UTC](https://www.ruby-forum.com/t/eval-and-dynamic-local-variable-creation-in-ruby-1-8-vs-1-9/139866 "2008-06-08T12:57:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thairuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a4c791/32.png) [@thairuby](https://www.ruby-forum.com/u/thairuby)\
**Post date:** [June 8, 2008, 12:57pm UTC](https://www.ruby-forum.com/t/eval-and-dynamic-local-variable-creation-in-ruby-1-8-vs-1-9/139866/1 "2008-06-08T12:57:45Z")

</div>

I tested this script with “Try Ruby! (in your browser)”  
([http://tryruby.hobix.com/](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.

---

<div class="post-metadata">

**Author:** ![thairuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a4c791/32.png) [@thairuby](https://www.ruby-forum.com/u/thairuby)\
**Post date:** [June 8, 2008, 7:07pm UTC](https://www.ruby-forum.com/t/eval-and-dynamic-local-variable-creation-in-ruby-1-8-vs-1-9/139866/2 "2008-06-08T19:07:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![thairuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a4c791/32.png) [@thairuby](https://www.ruby-forum.com/u/thairuby)\
**Post date:** [June 8, 2008, 7:19pm UTC](https://www.ruby-forum.com/t/eval-and-dynamic-local-variable-creation-in-ruby-1-8-vs-1-9/139866/3 "2008-06-08T19:19:35Z")

</div>

Hi,

In message “Re: eval and dynamic local variable creation in Ruby 1.8 vs  
1.9”  
on Sun, 8 Jun 2008 19:56:25 +0900, Thairuby Thairuby  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|I tested this script with “Try Ruby! (in your browser)”  
|([http://tryruby.hobix.com/](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?

The local variables are created in compile time, so that local  
variables that are defined in eval() cannot be accessed outside of  
eval. In 1.8, irb and tryruby does line by line compilation so that  
local variables are spilled from eval(), but in 1.9, it’s strictly  
prohibited even under line-by-line compilation.

```
          matz.

```
