IronShay

Ironing code, geek t-shirts and even presentations!

NAVIGATION - SEARCH

Executing IronPython Code from IronRuby

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:

class MyPythonClass:
  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.

Note

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
puts python.welcome_message
# Set the python variable
python.welcome_message = "Hello from Ruby!"
# Print its new value
puts python.welcome_message

 

The output is:
6
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,
Shay.

kick it on DotNetKicks.com Shout it



Comments (2) -

That's strange that it works. That execution isn't valid in Python.

In Python, you can't call MyPythonClass.add unless add is a classmethod or staticmethod. You would instead have to first create an instance, then call add on the instance.

my_python_object = MyPythonClass()
my_python_object.add(1,5)

Is it because it's Ruby that an instance is automatically created, or is the DLR performing a loose (unchecked) interpretation? Or in other words, what is self in the add method?

Reply

In Ruby you don't have to use parenthesis. This is why python_class = python.MyPythonClass works - it's the same as python_class = python.MyPythonClass().
Anyway I've changed it (added the parenthesis) to avoid confusion.

self in the add method, as I mentioned in the note, is the caller class instance. It is needed because of the way the DLR invokes methods.

Reply

Pingbacks and trackbacks (1)+

Add comment