[IronPython] IronPython and IronRuby interop with IronRuby 0.9

Michael Foord fuzzyman at voidspace.org.uk
Sat Aug 22 18:05:26 CEST 2009


Thanks Tomas.

Michael

Tomas Matousek wrote:
> This works:
>
> import clr
> clr.AddReference('IronRuby')
>
> from IronRuby import Ruby
> from System import Array
>
> paths = [r'C:\Binaries\IronRuby\lib\IronRuby', r'C:\Binaries\IronRuby\lib\ruby\1.8']
> array = Array[str](paths)
>
> engine = Ruby.CreateEngine()
> engine.SetSearchPaths(array)
>
> scope = engine.CreateScope()
> source = engine.Execute('''
> require 'date'
>
> def d
>   Date::civil(2003, 4, 8)
> end
> ''', scope)
>
> print scope.d()
> print engine.Runtime.Globals.Date
>
> ---
>
> Top-level methods, not local variables, are published in the scope.
> Classes and modules are published in Runtime.Globals scope.
>
> And this works too (the value of the last expression is returned):
> print engine.Execute('''
> require 'date'
> Date::civil(2003, 4, 8)
> ''')
>
> Tomas
>
> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Michael Foord
> Sent: Saturday, August 22, 2009 8:46 AM
> To: Discussion of IronPython
> Subject: Re: [IronPython] IronPython and IronRuby interop with IronRuby 0.9
>
> Hehe - well I can fish the contents of the library I required from engine.Runtime.Globals, which seems right as I'm requiring it in the global namespace. I'm still surprised the ScriptScope is empty.
>
> Michael
>
>
> Michael Foord wrote:
>   
>> Ok, so setting the engine search paths solves the failure to find the
>> library, but the ScriptScope is still coming back empty. In the
>> example below I would have expected to see 'd' in the ScriptScope.
>>
>> c:\Binaries\IronRuby\bin>ipy.exe interop.py []
>>
>> From this code:
>>
>> import clr
>> clr.AddReference('IronRuby')
>> clr.AddReference('Microsoft.Scripting')
>>
>> from System import Array
>>
>> paths = [r'C:\Binaries\IronRuby\lib\IronRuby',
>> r'C:\Binaries\IronRuby\lib\ruby\1.8']
>> array = Array[str](paths)
>>
>> source_code = "require 'date'\nd = Date::civil(2003, 4, 8)\n"
>>
>> from Microsoft.Scripting import SourceCodeKind from IronRuby import
>> Ruby engine = Ruby.CreateEngine()
>> engine.SetSearchPaths(array)
>> source = engine.CreateScriptSourceFromString(source_code,
>> SourceCodeKind.Statements)
>> scope = engine.CreateScope()
>> source.Execute(scope)
>>
>> print dir(scope)
>>
>> Michael
>>
>> 2009/8/22 Michael Foord <fuzzyman at voidspace.org.uk
>> <mailto:fuzzyman at voidspace.org.uk>>
>>
>>     Hello all,
>>
>>     I've played a little bit with IronPython and IronRuby interop with
>>     the IronRuby 0.9 binaries.
>>
>>     A very basic example works as expected:
>>
>>     IronPython 2.6 Beta 2 (2.6.0.20) on .NET 2.0.50727.4927
>>     Type "help", "copyright", "credits" or "license" for more information.
>>     >>> import clr
>>     >>> clr.AddReference('IronRuby')
>>     >>> from IronRuby import Ruby
>>     >>>
>>     >>> engine = Ruby.CreateEngine()
>>     >>> source = engine.CreateScriptSourceFromString("puts 'Hello from
>>     Ruby'")
>>     >>> scope = engine.CreateScope()
>>     >>>
>>     >>> source.Execute(scope)
>>     Hello from Ruby
>>     >>>
>>
>>
>>     However my attempts to use a Ruby library fails. The same code
>>     works when executed from ir.exe:
>>
>>     >>> import clr
>>     >>> clr.AddReference('IronRuby')
>>     >>> clr.AddReference('Microsoft.Scripting')
>>     >>>
>>     >>> from Microsoft.Scripting import SourceCodeKind
>>     >>> from IronRuby import Ruby
>>     >>> engine = Ruby.CreateEngine()
>>     >>> source = engine.CreateScriptSourceFromString("require 'date'",
>>     SourceCodeKin
>>     d.Statements)
>>     >>> scope = engine.CreateScope()
>>     >>> source.Execute(scope)
>>     Traceback (most recent call last):
>>      File "<stdin>", line 1, in <module>
>>     Exception: no such file to load -- date
>>     >>>
>>
>>     I tried adding a reference to IronRuby.Libraries to the runtime
>>     associated with the Ruby engine (using runtime.LoadAssembly) but
>>     this didn't help.
>>
>>     Requiring Ruby modules I've written myself doesn't blow-up but
>>     doesn't populate the scriptscope they are executed in with
>>     anything. Likewise calling engine.ExecuteFile('foo.rb') returns an
>>     empty ScriptScope.
>>
>>     Any ideas?
>>
>>     All the best,
>>
>>     Michael Foord
>>
>>     --
>>     http://www.ironpythoninaction.com/
>>     http://www.voidspace.org.uk/blog
>>
>>
>>
>>
>>
>> --
>> http://www.ironpythoninaction.com/
>>
>>
>> ----------------------------------------------------------------------
>> --
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>>     
>
>
> --
> http://www.ironpythoninaction.com/
> http://www.voidspace.org.uk/blog
>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>   


-- 
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog





More information about the Ironpython-users mailing list