[Ironruby-core] Ruby code from C-Sharp; String.split() with rbx

Stefan Hüttenrauch stefan.huettenrauch at hpi.uni-potsdam.de
Sun Sep 16 06:03:16 EDT 2007


Hi all,

 

I am playing around with IronRuby for a week now and have 3 questions/issues
that I sincerely hope you have some nice comments or solutions for.

Thanks for reading. I appreciate it.

Cheers,

Stefan

 

 

FIRST ISSUE

=============================

I try to call some ruby code from a c# file. My first attempt was this:

1 RubyEngine re = RubyEngine.CurrentEngine;

2 re.ExecuteCommand("s = 'let us get started'");

3 re.ExecuteCommand("i = s.length");

 

Problem and question:

I think each line is executed as a block, so that the second time I try to use
s it is undefined and I get an exception. If s is global it works. 

Is there a way to tell the RubyEngine to kind of collect Commands and then
execute them as one single block?

 

 

SECOND ISSUE

=============================

My second attempt to call ruby code from a c# file was this:

1 RubyEngine re = RubyEngine.CurrentEngine;

2 re.ExecuteCommand("require
'C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\mscorlib.dll'");

3 re.ExecuteFileContent("c:\\myRubyFile.rb");

 

myRubyFile.rb is:

1 require "C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\mscorlib.dll"

2 require
"C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\System.Windows.Forms.dll" 

3 sr = System::IO::StreamReader

4 sr = System::IO::File.OpenText("c:\\atlog.txt")

...

 

Problem:

Just executing the ruby file is not much of a problem; so calling ruby that in
turn calls the CLR works fine. But, if I include line 2 of the c# code above I
get the exception below, not telling me which file couldn't be found. Removing
line 2 brings me back to a running program. So I think with requiring a dll
twice is a problem. Should it be??

 

Ruby.Builtins.LoadError was unhandled

Message="File not found"

Source="Ruby"

StackTrace:

       at Ruby.Runtime.Loader.LoadFile(CodeContext context, Object self, String
path, LoadFlags flags)

       at Ruby.Builtins.Kernel.Require(CodeContext context, Object self, String
libraryName)

       at Ruby.Builtins.Kernel.Require(CodeContext context, Object self,
MutableString libraryName)

       at _stub_##8(Object[] , DynamicSite`3 , CodeContext , Object ,
MutableString )

       at Microsoft.Scripting.Actions.DynamicSiteTarget`3.Invoke(DynamicSite`3
site, CodeContext context, T0 arg0, T1 arg1)

       at
Microsoft.Scripting.Actions.DynamicSite`3.UpdateBindingAndInvoke(CodeContext
context, T0 arg0, T1 arg1)

       at
Microsoft.Scripting.Actions.DynamicSiteHelpers.UninitializedTargetHelper`7.Invo
ke2(DynamicSite`3 site, CodeContext context, T0 arg0, T1 arg1)

       at Microsoft.Scripting.Actions.DynamicSite`3.Invoke(CodeContext context,
T0 arg0, T1 arg1)

       at ##10(Object[] , CodeContext )

       at Microsoft.Scripting.CallTargetWithContext0.Invoke(CodeContext
context)

       at Microsoft.Scripting.ScriptCode.Run(CodeContext codeContext, Boolean
tryEvaluate)

       at Microsoft.Scripting.ScriptCode.Run(ScriptModule module)

       at Microsoft.Scripting.Hosting.CompiledCode.Evaluate(IScriptModule
module)

       at
Microsoft.Scripting.Hosting.ScriptEngine.ExecuteInteractiveCode(String code,
IScriptModule module)

       at Microsoft.Scripting.Hosting.ScriptEngine.ExecuteCommand(String code,
IScriptModule module)

       at Microsoft.Scripting.Hosting.ScriptEngine.ExecuteCommand(String code)

       at RubyFromCSharp.Program.Main(String[] args) in C:\Workspace
dotnet\RubyFromCSharp\RubyFromCSharp\Program.cs:line 23

       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)

       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)

       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

       at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)

       at System.Threading.ThreadHelper.ThreadStart()

 

I tried to debug this behavior. I set up the ironruby and Microsoft.scripting
project in VS2005 as it is described by Judah on
http://www.iunknown.com/2007/08/ironruby-on-rub.html. But adding a main to the
ironruby project and executing it as a console application gives me the
exception below. Why is that? I also recognize that using the
Microsoft.Scriptin.dll that is build from VS2005 throws this exception when
being used in another project. The same dll compiled with rake works fine.

 

System.IO.FileLoadException was unhandled

Message="Could not load file or assembly 'Microsoft.Scripting,
Version=1.0.0.400, Culture=neutral, PublicKeyToken=51c7b9aefdaeb546' or one of
its dependencies. Strong name validation failed. (Exception from HRESULT:
0x8013141A)"

Source="ConsoleApplication1"

FileName="Microsoft.Scripting, Version=1.0.0.400, Culture=neutral,
PublicKeyToken=51c7b9aefdaeb546"

FusionLog=""

StackTrace:

       at ConsoleApplication1.Program.Main(String[] args)

       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)

       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)

       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

       at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)

       at System.Threading.ThreadHelper.ThreadStart()

 

 

 

THIRD ISSUE

=============================

I don't know if this is a bug. Using split() with the standard ruby command
line is just fine. But using it with rbx throws an error. I also tried the
variant with two parameters. Same issue. 

 

C:\>irb

irb(main):001:0> s = 'hello world'

=> "hello world"

irb(main):002:0> t = s.split(' ')

=> ["hello", "world"]

irb(main):003:0> exit

 

C:\>rbx

IronRuby Pre-Alpha (1.0.0.0) on .NET 2.0.50727.42

copyright (c) Microsoft Corporation. All rights reserved.

>>> s = 'hello world'

=> "hello world"

>>> t = s.split(' ')

System.ArgumentException: wrong number or type of arguments for `split'

   at _stub_##6(Object[] , DynamicSite`3 , CodeContext , Object , MutableString
)

   at Microsoft.Scripting.Actions.DynamicSiteTarget`3.Invoke(DynamicSite`3
site, CodeContext context, T0 arg0, T1 arg1)

   at
Microsoft.Scripting.Actions.DynamicSite`3.UpdateBindingAndInvoke(CodeContext
context, T0 arg0, T1 arg1)

   at
Microsoft.Scripting.Actions.DynamicSiteHelpers.UninitializedTargetHelper`7.Invo
ke2(DynamicSite`3 site, CodeContext context, T0 arg0, T1 arg1)

   at Microsoft.Scripting.Actions.DynamicSite`3.Invoke(CodeContext context, T0
arg0, T1 arg1)

   at ##5(Object[] , CodeContext )

   at Microsoft.Scripting.CallTargetWithContext0.Invoke(CodeContext context)

   at Microsoft.Scripting.ScriptCode.Run(CodeContext codeContext, Boolean
tryEvaluate)

   at Microsoft.Scripting.ScriptCode.Run(ScriptModule module)

   at Microsoft.Scripting.Hosting.CompiledCode.Evaluate(IScriptModule module)

   at Microsoft.Scripting.Hosting.ScriptEngine.ExecuteCommand(String code,
IScriptModule module)

   at Microsoft.Scripting.Shell.CommandLine.RunOneInteraction()

   at Microsoft.Scripting.Shell.CommandLine.TryInteractiveAction()

   at Microsoft.Scripting.Shell.CommandLine.RunInteractiveLoop()

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/ironruby-core/attachments/20070916/2f5b610c/attachment-0001.html 


More information about the Ironruby-core mailing list