Ben Hall
2008-Jun-24 00:23 UTC
[Ironruby-core] ExecutionContext without references IronRuby
Hi,
This might be a bit of a strange request, but is it possible to get
access to the ExecutionContext without actually referencing the
IronRuby assembly?
As a quick demo, I wrote the following code:
ScriptRuntime runtime = IronRuby.CreateRuntime();
engine = IronRuby.GetEngine(runtime);
RubyExecutionContext ctx = IronRuby.GetExecutionContext(engine);
list = new List<string>();
ctx.DefineGlobalVariable("customers", list);
Ideally, what I wanted to write was something more like but $customers
is always nil.
scope = engine.CreateScope();
scope.SetVariable("$customers", list);
I was hoping this would have had the same affect as the code above.
Is this by design, a limitation or a bug?
Thanks
Ben
Blog.BenHall.me.uk
Tomas Matousek
2008-Jun-24 00:50 UTC
[Ironruby-core] ExecutionContext without references IronRuby
This is by design. Ruby global variables are Ruby special feature, not all
languages have such thing.
You can however use the scope and completely avoid statically referencing
IronRuby assemblies:
var runtime = ScriptRuntime.Create();
var scope = runtime.CreateScope("Ruby");
scope.SetVariable("customers", list);
scope.Execute("p customers");
In this example, "customers" is a method call that hits
method_missing. The IronRuby''s implementation of method_missing looks
into the scope if run in a hosted environment.
Tomas
-----Original Message-----
From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at
rubyforge.org] On Behalf Of Ben Hall
Sent: Monday, June 23, 2008 5:23 PM
To: ironruby-core at rubyforge.org
Subject: [Ironruby-core] ExecutionContext without references IronRuby
Hi,
This might be a bit of a strange request, but is it possible to get
access to the ExecutionContext without actually referencing the
IronRuby assembly?
As a quick demo, I wrote the following code:
ScriptRuntime runtime = IronRuby.CreateRuntime();
engine = IronRuby.GetEngine(runtime);
RubyExecutionContext ctx = IronRuby.GetExecutionContext(engine);
list = new List<string>();
ctx.DefineGlobalVariable("customers", list);
Ideally, what I wanted to write was something more like but $customers
is always nil.
scope = engine.CreateScope();
scope.SetVariable("$customers", list);
I was hoping this would have had the same affect as the code above.
Is this by design, a limitation or a bug?
Thanks
Ben
Blog.BenHall.me.uk
_______________________________________________
Ironruby-core mailing list
Ironruby-core at rubyforge.org
http://rubyforge.org/mailman/listinfo/ironruby-core
Ben Hall
2008-Jun-24 01:20 UTC
[Ironruby-core] ExecutionContext without references IronRuby
Thanks for your reply. The fact "customers" calls method_missing is a
bit of a problem, as what happens if you call customers within the
method_missing block?
scope = engine.CreateScope();
scope.SetVariable("customers", list);
string startingBlock = @"require ''mscorlib''
def
customers.method_missing(methodname, *args)
puts methodname
if(methodname.to_s =~ /Bob/)
customers.add(methodname.to_s)
else
super
end
end
customers.Bob
";
scope.Execute(startingBlock);
I was working based on Phil Haack''s Monkey Patching example and seeing
what happened.
I put some code which checks to see if the methodname is itself, but
then I check exceptions saying add isn''t on NilClass, must be doing
something wrong in that scenario
Code I added to the method was:
if( methodname.to_s == ''customers'')
self
Like I said, only playing around to see what happens but I can see
people possible wanting to do this.
On Tue, Jun 24, 2008 at 1:50 AM, Tomas Matousek
<Tomas.Matousek at microsoft.com> wrote:> This is by design. Ruby global variables are Ruby special feature, not all
languages have such thing.
> You can however use the scope and completely avoid statically referencing
IronRuby assemblies:
>
> var runtime = ScriptRuntime.Create();
> var scope = runtime.CreateScope("Ruby");
> scope.SetVariable("customers", list);
> scope.Execute("p customers");
>
> In this example, "customers" is a method call that hits
method_missing. The IronRuby''s implementation of method_missing looks
into the scope if run in a hosted environment.
>
> Tomas
>
> -----Original Message-----
> From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces
at rubyforge.org] On Behalf Of Ben Hall
> Sent: Monday, June 23, 2008 5:23 PM
> To: ironruby-core at rubyforge.org
> Subject: [Ironruby-core] ExecutionContext without references IronRuby
>
> Hi,
>
> This might be a bit of a strange request, but is it possible to get
> access to the ExecutionContext without actually referencing the
> IronRuby assembly?
>
> As a quick demo, I wrote the following code:
>
> ScriptRuntime runtime = IronRuby.CreateRuntime();
> engine = IronRuby.GetEngine(runtime);
> RubyExecutionContext ctx = IronRuby.GetExecutionContext(engine);
>
> list = new List<string>();
> ctx.DefineGlobalVariable("customers", list);
>
> Ideally, what I wanted to write was something more like but $customers
> is always nil.
>
> scope = engine.CreateScope();
> scope.SetVariable("$customers", list);
>
> I was hoping this would have had the same affect as the code above.
>
> Is this by design, a limitation or a bug?
>
> Thanks
>
> Ben
> Blog.BenHall.me.uk
> _______________________________________________
> Ironruby-core mailing list
> Ironruby-core at rubyforge.org
> http://rubyforge.org/mailman/listinfo/ironruby-core
>
> _______________________________________________
> Ironruby-core mailing list
> Ironruby-core at rubyforge.org
> http://rubyforge.org/mailman/listinfo/ironruby-core
>
Tomas Matousek
2008-Jun-24 01:34 UTC
[Ironruby-core] ExecutionContext without references IronRuby
We might define some library method that will look-up the scope''s
variables explicitly to enable this scenario.
As a workaround, this should work:
string startingBlock = @"
$scope = self
p $scope.customers
";
string startingBlock = @"require ''mscorlib''
def
customers.method_missing(methodname, *args)
puts methodname
if(methodname.to_s =~ /Bob/)
customers.add(methodname.to_s)
else
super
end
end
customers.Bob
";
-----Original Message-----
From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at
rubyforge.org] On Behalf Of Ben Hall
Sent: Monday, June 23, 2008 6:21 PM
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] ExecutionContext without references IronRuby
Thanks for your reply. The fact "customers" calls method_missing is a
bit of a problem, as what happens if you call customers within the
method_missing block?
scope = engine.CreateScope();
scope.SetVariable("customers", list);
string startingBlock = @"require ''mscorlib''
def
customers.method_missing(methodname, *args)
puts methodname
if(methodname.to_s =~ /Bob/)
customers.add(methodname.to_s)
else
super
end
end
customers.Bob
";
scope.Execute(startingBlock);
I was working based on Phil Haack''s Monkey Patching example and seeing
what happened.
I put some code which checks to see if the methodname is itself, but
then I check exceptions saying add isn''t on NilClass, must be doing
something wrong in that scenario
Code I added to the method was:
if( methodname.to_s == ''customers'')
self
Like I said, only playing around to see what happens but I can see
people possible wanting to do this.
On Tue, Jun 24, 2008 at 1:50 AM, Tomas Matousek
<Tomas.Matousek at microsoft.com> wrote:> This is by design. Ruby global variables are Ruby special feature, not all
languages have such thing.
> You can however use the scope and completely avoid statically referencing
IronRuby assemblies:
>
> var runtime = ScriptRuntime.Create();
> var scope = runtime.CreateScope("Ruby");
> scope.SetVariable("customers", list);
> scope.Execute("p customers");
>
> In this example, "customers" is a method call that hits
method_missing. The IronRuby''s implementation of method_missing looks
into the scope if run in a hosted environment.
>
> Tomas
>
> -----Original Message-----
> From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces
at rubyforge.org] On Behalf Of Ben Hall
> Sent: Monday, June 23, 2008 5:23 PM
> To: ironruby-core at rubyforge.org
> Subject: [Ironruby-core] ExecutionContext without references IronRuby
>
> Hi,
>
> This might be a bit of a strange request, but is it possible to get
> access to the ExecutionContext without actually referencing the
> IronRuby assembly?
>
> As a quick demo, I wrote the following code:
>
> ScriptRuntime runtime = IronRuby.CreateRuntime();
> engine = IronRuby.GetEngine(runtime);
> RubyExecutionContext ctx = IronRuby.GetExecutionContext(engine);
>
> list = new List<string>();
> ctx.DefineGlobalVariable("customers", list);
>
> Ideally, what I wanted to write was something more like but $customers
> is always nil.
>
> scope = engine.CreateScope();
> scope.SetVariable("$customers", list);
>
> I was hoping this would have had the same affect as the code above.
>
> Is this by design, a limitation or a bug?
>
> Thanks
>
> Ben
> Blog.BenHall.me.uk
> _______________________________________________
> Ironruby-core mailing list
> Ironruby-core at rubyforge.org
> http://rubyforge.org/mailman/listinfo/ironruby-core
>
> _______________________________________________
> Ironruby-core mailing list
> Ironruby-core at rubyforge.org
> http://rubyforge.org/mailman/listinfo/ironruby-core
>
_______________________________________________
Ironruby-core mailing list
Ironruby-core at rubyforge.org
http://rubyforge.org/mailman/listinfo/ironruby-core
Tomas Matousek
2008-Jun-24 01:37 UTC
[Ironruby-core] ExecutionContext without references IronRuby
Sorry, pressed Ctrl+Enter before finished.
You can use global $scope.customers in your implementation of method_missing to
access customers.
Tomas
-----Original Message-----
We might define some library method that will look-up the scope''s
variables explicitly to enable this scenario.
As a workaround, this should work:
string startingBlock = @"
$scope = self
p $scope.customers
";
string startingBlock = @"require ''mscorlib''
def
customers.method_missing(methodname, *args)
puts methodname
if(methodname.to_s =~ /Bob/)
customers.add(methodname.to_s)
else
super
end
end
customers.Bob
";
-----Original Message-----
From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at
rubyforge.org] On Behalf Of Ben Hall
Sent: Monday, June 23, 2008 6:21 PM
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] ExecutionContext without references IronRuby
Thanks for your reply. The fact "customers" calls method_missing is a
bit of a problem, as what happens if you call customers within the
method_missing block?
scope = engine.CreateScope();
scope.SetVariable("customers", list);
string startingBlock = @"require ''mscorlib''
def
customers.method_missing(methodname, *args)
puts methodname
if(methodname.to_s =~ /Bob/)
customers.add(methodname.to_s)
else
super
end
end
customers.Bob
";
scope.Execute(startingBlock);
I was working based on Phil Haack''s Monkey Patching example and seeing
what happened.
I put some code which checks to see if the methodname is itself, but
then I check exceptions saying add isn''t on NilClass, must be doing
something wrong in that scenario
Code I added to the method was:
if( methodname.to_s == ''customers'')
self
Like I said, only playing around to see what happens but I can see
people possible wanting to do this.
On Tue, Jun 24, 2008 at 1:50 AM, Tomas Matousek
<Tomas.Matousek at microsoft.com> wrote:> This is by design. Ruby global variables are Ruby special feature, not all
languages have such thing.
> You can however use the scope and completely avoid statically referencing
IronRuby assemblies:
>
> var runtime = ScriptRuntime.Create();
> var scope = runtime.CreateScope("Ruby");
> scope.SetVariable("customers", list);
> scope.Execute("p customers");
>
> In this example, "customers" is a method call that hits
method_missing. The IronRuby''s implementation of method_missing looks
into the scope if run in a hosted environment.
>
> Tomas
>
> -----Original Message-----
> From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces
at rubyforge.org] On Behalf Of Ben Hall
> Sent: Monday, June 23, 2008 5:23 PM
> To: ironruby-core at rubyforge.org
> Subject: [Ironruby-core] ExecutionContext without references IronRuby
>
> Hi,
>
> This might be a bit of a strange request, but is it possible to get
> access to the ExecutionContext without actually referencing the
> IronRuby assembly?
>
> As a quick demo, I wrote the following code:
>
> ScriptRuntime runtime = IronRuby.CreateRuntime();
> engine = IronRuby.GetEngine(runtime);
> RubyExecutionContext ctx = IronRuby.GetExecutionContext(engine);
>
> list = new List<string>();
> ctx.DefineGlobalVariable("customers", list);
>
> Ideally, what I wanted to write was something more like but $customers
> is always nil.
>
> scope = engine.CreateScope();
> scope.SetVariable("$customers", list);
>
> I was hoping this would have had the same affect as the code above.
>
> Is this by design, a limitation or a bug?
>
> Thanks
>
> Ben
> Blog.BenHall.me.uk
> _______________________________________________
> Ironruby-core mailing list
> Ironruby-core at rubyforge.org
> http://rubyforge.org/mailman/listinfo/ironruby-core
>
> _______________________________________________
> Ironruby-core mailing list
> Ironruby-core at rubyforge.org
> http://rubyforge.org/mailman/listinfo/ironruby-core
>
_______________________________________________
Ironruby-core mailing list
Ironruby-core at rubyforge.org
http://rubyforge.org/mailman/listinfo/ironruby-core
_______________________________________________
Ironruby-core mailing list
Ironruby-core at rubyforge.org
http://rubyforge.org/mailman/listinfo/ironruby-core