# scope.SetVariable weirdness with instance\_eval

**URL:** <https://www.ruby-forum.com/t/scope-setvariable-weirdness-with-instance-eval/179214>\
**Category:** IronRuby\
**Created:** [November 19, 2009, 7:32pm UTC](https://www.ruby-forum.com/t/scope-setvariable-weirdness-with-instance-eval/179214 "2009-11-19T19:32:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![barby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@barby](https://www.ruby-forum.com/u/barby)\
**Post date:** [November 19, 2009, 7:32pm UTC](https://www.ruby-forum.com/t/scope-setvariable-weirdness-with-instance-eval/179214/1 "2009-11-19T19:32:36Z")

</div>

Hi

Consider the following code:

public class Item  
{  
public string Title {  
get;  
private set;  
}

public Item(string title){ Title = title; }  
}

var \_engine = Ruby.CreateEngine();  
var context = new Item(“The greatest item ever”);

var scope = \_engine.CreateScope();  
_scope.SetVariable(“ctxt”, item);_  
\_engine.ExecuteFile(“path\to\file.rb”, scope);

And the following ruby class:

class Testing

def initialize(&b)  
instance\_eval(&b)  
end

def context(ctxt)  
@ctxt = ctxt  
end

def print  
puts @ctxt.title  
end

end

Then this works when put at the bottom of the file:

_inserted = ctxt_  
Testing.new do  
_context inserted_  
print  
end

but this doesn’t:

Testing.new do  
_context ctxt_  
print  
end

That doesn’t seem right or does it? so I’ve created an issue on codeplex  
with this content, if this is the way it’s supposed to work then I’ll  
delete  
the issue

* * *

Met vriendelijke groeten - Best regards - Salutations  
Ivan Porto C.  
Blog: [http://flanders.co.nz](http://flanders.co.nz)  
Google Wave: removed\_email\_address@domain.invalid  
Twitter: [http://twitter.com/casualjim](http://twitter.com/casualjim)  
Author of IronRuby in Action ([http://manning.com/carrero](http://manning.com/carrero))

---

<div class="post-metadata">

**Author:** ![barby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@barby](https://www.ruby-forum.com/u/barby)\
**Post date:** [November 19, 2009, 7:46pm UTC](https://www.ruby-forum.com/t/scope-setvariable-weirdness-with-instance-eval/179214/2 "2009-11-19T19:46:38Z")

</div>

inserted = ctxt

calls a â€œctxtâ€ method on â€œselfâ€. The hosting API injects method\_missing  
into the top-level singleton â€“ you can see it by printing p  
self.method(:method\_missing).  
The implementation of that method\_missing looks into the scope for  
variables.

In the latter case, I assume, you instance\_eval the block against some  
object, right? Hence ctxt is an invocation on that object, which has no  
relationship with the scope.

Testing.new do  
context ctxt  
print  
end

Tomas

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Ivan Porto  
Carrero  
Sent: Thursday, November 19, 2009 10:21 AM  
To: ironruby-core  
Subject: [Ironruby-core] scope.SetVariable weirdness with instance\_eval

Hi

Consider the following code:

public class Item  
{  
public string Title {  
get;  
private set;  
}

public Item(string title){ Title = title; }  
}

var \_engine = Ruby.CreateEngine();  
var context = new Item(“The greatest item ever”);

var scope = \_engine.CreateScope();  
scope.SetVariable(“ctxt”, item);  
\_engine.ExecuteFile(“path\to\file.rb”, scope);

And the following ruby class:

class Testing

def initialize(&b)  
instance\_eval(&b)  
end

def context(ctxt)  
@ctxt = ctxt  
end

def print  
puts @ctxt.title  
end

end

Then this works when put at the bottom of the file:

inserted = ctxt  
Testing.new do  
context inserted  
print  
end

but this doesn’t:

Testing.new do  
context ctxt  
print  
end

That doesn’t seem right or does it? so I’ve created an issue on codeplex  
with this content, if this is the way it’s supposed to work then I’ll  
delete the issue

* * *

Met vriendelijke groeten - Best regards - Salutations  
Ivan Porto C.  
Blog: [http://flanders.co.nz](http://flanders.co.nz)  
Google Wave:  
removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Twitter: [http://twitter.com/casualjim](http://twitter.com/casualjim)  
Author of IronRuby in Action ([http://manning.com/carrero](http://manning.com/carrero))

---

<div class="post-metadata">

**Author:** ![barby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@barby](https://www.ruby-forum.com/u/barby)\
**Post date:** [November 19, 2009, 8:04pm UTC](https://www.ruby-forum.com/t/scope-setvariable-weirdness-with-instance-eval/179214/3 "2009-11-19T20:04:48Z")

</div>

I tried closing the issue on codeplex but couldn’t

[http://ironruby.codeplex.com/WorkItem/View.aspx?WorkItemId=3136](http://ironruby.codeplex.com/WorkItem/View.aspx?WorkItemId=3136)

## Thanks for the explanation

Met vriendelijke groeten - Best regards - Salutations  
Ivan Porto C.  
Blog: [http://flanders.co.nz](http://flanders.co.nz)  
Google Wave: removed\_email\_address@domain.invalid  
Twitter: [http://twitter.com/casualjim](http://twitter.com/casualjim)  
Author of IronRuby in Action ([http://manning.com/carrero](http://manning.com/carrero))

On Thu, Nov 19, 2009 at 7:45 PM, Tomas M. \<

---

<div class="post-metadata">

**Author:** ![barby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@barby](https://www.ruby-forum.com/u/barby)\
**Post date:** [November 19, 2009, 8:54pm UTC](https://www.ruby-forum.com/t/scope-setvariable-weirdness-with-instance-eval/179214/4 "2009-11-19T20:54:33Z")

</div>

I just assumed (\<== error right there) that

scope.SetVariable(“the\_var”, “the value”);

did the same under the covers as

the\_var = “the value”

so that it would be a real globally scoped variable, this probably makes  
more sense once you know how it works. This is also perfect timing for a  
call-out in this particular chapter 🙂 Tomas says…

But I’ve been caught out a few times now, I think I just need to take  
some  
time to look into the internals of IronRuby again, because I can figure  
this  
sort of stuff out by looking at the source code.

I have no bright ideas at this point, I rarely do.

* * *

Met vriendelijke groeten - Best regards - Salutations  
Ivan Porto C.  
Blog: [http://flanders.co.nz](http://flanders.co.nz)  
Google Wave: removed\_email\_address@domain.invalid  
Twitter: [http://twitter.com/casualjim](http://twitter.com/casualjim)  
Author of IronRuby in Action ([http://manning.com/carrero](http://manning.com/carrero))

On Thu, Nov 19, 2009 at 8:12 PM, Tomas M. \<

---

<div class="post-metadata">

**Author:** ![barby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@barby](https://www.ruby-forum.com/u/barby)\
**Post date:** [November 19, 2009, 8:12pm UTC](https://www.ruby-forum.com/t/scope-setvariable-weirdness-with-instance-eval/179214/5 "2009-11-19T20:12:59Z")

</div>

Iâ€™ve closed it. If you think there might be a better approach to  
exposing scope variables let us know.

Tomas

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Ivan Porto  
Carrero  
Sent: Thursday, November 19, 2009 10:51 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] scope.SetVariable weirdness with  
instance\_eval

I tried closing the issue on codeplex but couldn’t

[http://ironruby.codeplex.com/WorkItem/View.aspx?WorkItemId=3136](http://ironruby.codeplex.com/WorkItem/View.aspx?WorkItemId=3136)

## Thanks for the explanation

Met vriendelijke groeten - Best regards - Salutations  
Ivan Porto C.  
Blog: [http://flanders.co.nz](http://flanders.co.nz)  
Google Wave:  
removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Twitter: [http://twitter.com/casualjim](http://twitter.com/casualjim)  
Author of IronRuby in Action ([http://manning.com/carrero](http://manning.com/carrero))

On Thu, Nov 19, 2009 at 7:45 PM, Tomas M.  
\<removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\>  
wrote:  
inserted = ctxt

calls a â€œctxtâ€ method on â€œselfâ€. The hosting API injects method\_missing  
into the top-level singleton â€“ you can see it by printing p  
self.method(:method\_missing).  
The implementation of that method\_missing looks into the scope for  
variables.

In the latter case, I assume, you instance\_eval the block against some  
object, right? Hence ctxt is an invocation on that object, which has no  
relationship with the scope.

Testing.new do  
context ctxt  
print  
end

Tomas

From:  
removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
On Behalf Of Ivan Porto C.  
Sent: Thursday, November 19, 2009 10:21 AM  
To: ironruby-core  
Subject: [Ironruby-core] scope.SetVariable weirdness with instance\_eval

Hi

Consider the following code:

public class Item  
{  
public string Title {  
get;  
private set;  
}

public Item(string title){ Title = title; }  
}

var \_engine = Ruby.CreateEngine();  
var context = new Item(“The greatest item ever”);

var scope = \_engine.CreateScope();  
scope.SetVariable(“ctxt”, item);  
\_engine.ExecuteFile(“path\to\file.rb”, scope);

And the following ruby class:

class Testing

def initialize(&b)  
instance\_eval(&b)  
end

def context(ctxt)  
@ctxt = ctxt  
end

def print  
puts @ctxt.title  
end

end

Then this works when put at the bottom of the file:

inserted = ctxt  
Testing.new do  
context inserted  
print  
end

but this doesn’t:

Testing.new do  
context ctxt  
print  
end

That doesn’t seem right or does it? so I’ve created an issue on codeplex  
with this content, if this is the way it’s supposed to work then I’ll  
delete the issue

* * *

Met vriendelijke groeten - Best regards - Salutations  
Ivan Porto C.  
Blog: [http://flanders.co.nz](http://flanders.co.nz)  
Google Wave:  
removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Twitter: [http://twitter.com/casualjim](http://twitter.com/casualjim)  
Author of IronRuby in Action ([http://manning.com/carrero](http://manning.com/carrero))

* * *

Ironruby-core mailing list  
removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)
