- Posted by Shay Friedman on November 25, 2009
One of the advantages of the Dynamic Language Runtime (DLR) is the fact that it makes sharing code between the languages that are written on top of it (and on top of the CLR as well). Therefore, it is possible to share code between IronPython and IronRuby (and any other DLR language as well like IronScheme).
This means that IronPython libraries can be used from IronRuby code and vice versa. Ruby on Rails in Python? Django in Ruby? feels like the end of days, isn’t it? perhaps we should really start preparing to year 2012…
In this post I’ll show you how to run simple IronPython code from IronRuby so you can take it and do whatever your imagination guides you to.
Assuming we have a demo.py IronPython file with the next content:
def add(self, x, y):
return x + y
welcome_message = "Hello from Python!"
To those of you who don’t know Python, the code above declares a class named MyPythonClass with a single method named add that combines two numbers and return the result. It also contains a variable named welcome_message.
Because executing Python code from IronRuby involves DLR services, methods that you call from IronRuby must have self as their first argument just like IronRuby-targeted C# methods have (if the method accepts no parameters, then it should have only the self argument). This argument contains the caller class instance.
This means that python code that should be executed by IronRuby should be modified to match the requirements.
After we have the python file in place we can use it from IronRuby. The key for doing so is loading the python file using the IronRuby.require method. This method is similar to the Kernel#require method but with a small difference – it returns the DLR scope object of the loaded script. This enables you to call the script members via the scope, just like when you load a script manually via the DLR LoadFile method.
The next IronRuby is pretty straight-forward:
# Load the python file
python = IronRuby.require('demo.py')
# Get an instance of MyPythonClass
python_class = python.MyPythonClass()
# Execute the add method (pay attention that there's no
# need to pass the self parameter, this is done automatically)
puts python_class.add(1, 5)
# Get the python variable and print its value
# Set the python variable
python.welcome_message = "Hello from Ruby!"
# Print its new value
The output is:
Hello from Python!
Hello from Ruby!
Note that for this sample to run, you need IronRuby and IronPython installed on your machine, both compiled with the same Microsoft.Scripting project. I just compiled IronRuby’s and IronPython’s sources to make it work.
In conclusion, the DLR opens a bunch of new and exciting possibilities specifically in the field of code sharing between dynamic languages and dynamic and static languages. Go ahead and try it, it’s magical!
All the best,