# Code Review: RubyScopes2

**URL:** <https://www.ruby-forum.com/t/code-review-rubyscopes2/166745>\
**Category:** IronRuby\
**Created:** [April 29, 2009, 5:31am UTC](https://www.ruby-forum.com/t/code-review-rubyscopes2/166745 "2009-04-29T05:31:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![grujildo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@grujildo](https://www.ruby-forum.com/u/grujildo)\
**Post date:** [April 29, 2009, 5:31am UTC](https://www.ruby-forum.com/t/code-review-rubyscopes2/166745/1 "2009-04-29T05:31:00Z")

</div>

tfpt review “/shelveset:RubyScopes2;REDMOND\tomat”

DLR

Deletes IDynamicObject and GlobalsDictionary types and Scope.GetKeys  
method.  
Adds a default implementation for ScriptCode.Run that creates a new  
scope and calls Run(scope).  
Adds assertions to ContractUtils.Invariant.

Ruby

RubyScope fixes and refactoring.

This change enables top-level hosted code to define instance methods  
in DLR scope. It is no longer necessary to define them as singleton  
methods.  
Ruby engine executes the hosted code as if it was executed using  
instance\_eval on a proc. The behavior is the same except for control  
flow - break or retry on top-level throws an exception as it does in  
MRI.

This unit test illustrates the new behavior:

```
          // When executed without a scope top-level methods are 

```

defined on Object (as in MRI):  
Engine.Execute(“def foo; 1; end”);  
Assert(Context.ObjectClass.GetMethod(“foo”) != null);

```
          // When executed against a scope top-level methods are 

```

defined on main singleton (not on Object) and also stored in the scope:  
var scope = Engine.CreateScope();  
Engine.Execute(“def bar; 1; end”, scope);  
Assert(Context.ObjectClass.GetMethod(“bar”) == null);  
Assert(scope.GetVariable(“bar”) != null);

Fixes bug in top-level scope compilation - variables from the scope  
were not bound properly:

```
          var compiled = 

```

Engine.CreateScriptSourceFromString(“some\_variable”).Compile();  
scope = Engine.CreateScope();  
scope.SetVariable(“some\_variable”, “foo”);  
Assert(compiled.Execute(scope) == “foo”);

Tomas
