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

November 25. 2009 04:37 PM

Jason R. Coombs

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?

Jason R. Coombs

November 25. 2009 04:45 PM

Shay Friedman

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.

Shay Friedman

November 4. 2010 08:11 PM

trackback

IronRuby Posts Recap

IronRuby Posts Recap

IronShay

January 9. 2013 10:42 AM

interiordesigngroups.com

It is so much pleasurable blog. Because it is always refresh to read interiordesigngroups.com

interiordesigngroups.com

April 10. 2013 10:06 AM

www.healthreformcentral.org

"I always like to read a quality content having accurate information regarding the subject and the same thing I found in this post. Nice work."

www.healthreformcentral.org

April 16. 2013 10:59 AM

Student Loans

This conent is simply exciting and creative. I have been deciding on a institutional move and this has helped me with one aspect. Cheers!  <a href="www.educationendowmentfund.org/">Student Loans</a>

Student Loans

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Subscribe Subscribe

That's Me!

Hi! I'm Shay Friedman
I'm Shay Friedman - a Visual C#/IronRuby MVP, a consultant and instructor of .NET technologies, author, speaker and new technologies freak
More about me

Contact Me

> Contact page
> Twitter: @ironshay
> LinkedIn profile

Search

Hosted By

I'm hosting this site on Arvixe and I'm very happy with it.
If you're looking for ASP.NET hosting, I highly recommend it
(and if you order from this link I also get some beer money!)
Web Hosting