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
Google Wave: [email protected]
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

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: [email protected]
[mailto:[email protected]] 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
Google Wave:
[email protected]mailto:[email protected]
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

I tried closing the issue on codeplex but couldn’t

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
Google Wave: [email protected]
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

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

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 :slight_smile: 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
Google Wave: [email protected]
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

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

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

Tomas

From: [email protected]
[mailto:[email protected]] On Behalf Of Ivan Porto
Carrero
Sent: Thursday, November 19, 2009 10:51 AM
To: [email protected]
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

Thanks for the explanation

Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Google Wave:
[email protected]mailto:[email protected]
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

On Thu, Nov 19, 2009 at 7:45 PM, Tomas M.
<[email protected]mailto:[email protected]>
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:
[email protected]mailto:[email protected]
[mailto:[email protected]mailto:[email protected]]
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
Google Wave:
[email protected]mailto:[email protected]
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)


Ironruby-core mailing list
[email protected]mailto:[email protected]
http://rubyforge.org/mailman/listinfo/ironruby-core