Ziff-Davis Enterprise 
DevSource: Microsoft Developer Resource
Add OnsArchitectureLanguagesTechniquesUsing VSForums
 
Home arrow Languages arrow Page 6 - Five Things You Didn't Know You Could Do with Ruby
Five Things You Didn't Know You Could Do with Ruby
By Hal Fulton

Rate This Article:
Add This Article To:
Five Things You Didn't Know You Could Do with Ruby - ' An Application Interface '
( Page 6 of 6 )

Interface with Other Applications, Technologies, and Services

Let's face it: the computers on Star Trek are a lot easier to use than ours. That is because the issues of interoperability have supposedly been fully addressed and conquered by the 24th century.

ADVERTISEMENT

But in our century, we're still working on it. Software components and applications need to interoperate with each other, and in general they're much easier to integrate now than they were twenty years ago.

Frequently a Ruby programmer will need to leverage the power of an existing library or application while at the same time harnessing the power of Ruby itself. The answer, of course, is to create a Ruby binding for the library or app (which may have been written in C, C++, or some other language).

One way to accomplish this is to use Ruby's C API to write a binding or an extension. Many people find this API to be exceptionally easy and intuitive compared to the ones that some other languages expose; but since that is highly subjective, you can judge for yourself.

If you're familiar with SWIG, which seeks to automate the creation of bindings to C++ code, you can certainly use SWIG with Ruby. The Ruby wrapper code is part of the standard SWIG distribution.

To embed "foreign language" code in your Ruby, take a look at RubyInline. As the project page says, "Ruby Inline is an analog to Perl's Inline::C. Out of the box, it allows you to embed C/C++ external module code in your Ruby script directly. By writing simple builder classes, you can teach how to cope with new languages (Fortran, Perl, whatever)."

If the app or lib you want is common enough, there's a good chance that a Ruby binding already exists. To pick a few at random: There is a binding for GraphViz, used to analyze and draw mathematical graphs; and one for ImageMagick, used to create and manipulate images in JPEG, GIF, PNG and similar formats. If it's source control you want, there are Ruby bindings for both CVS and Subversion. There are Ruby tools for interacting with Google search, with Amazon, and with Pay Pal. If you're into GUI toolkits and frameworks, there are Ruby bindings for Qt, Tk, GTK+, Fox, and others. There are bindings for many popular libraries in such areas as sound, multimedia, document formatting, PDF and Flash manipulation, XML processing, YAML marshaling, Web services, and more.

It's hard to avoid databases nowadays. Ruby plays well with MySQL, PostgreSQL, Oracle, LDAP, and others. There is a DBI library for those who want to write DB-independent code.

If Java is your thing, you might like JRuby. This is essentially a reimplementation of the Ruby interpreter in Java; it interoperates seamlessly with Java libraries and tools. If that is a little too heavyweight for your needs, you might look at RJNI (an interface to JNI for Ruby); though this has definite limitations, it is still usable for some purposes. Finally, Rich Kilmer has demonstrated an implementation of JDWP (the Java Debug Wire Protocol) in Ruby; in 20,000 lines of Ruby, he created a framework that enabled the simultaneous controlling and monitoring of hundreds of Java agents "over the wire."

As for other VMs such as Parrot and .NET, these mostly fall into the "Watch This Space" category. No one is really using Parrot yet, since it isn't mature; but we expect things from it later. (Dan Sugalski, its lead designer, is a Ruby enthusiast.) As for .NET, a working Ruby/.NET "bridge" is usable for many purposes. Multiple projects are aiming at full integration of Ruby and .NET, though none of these is mature yet.

I've already mentioned XMLRPC. For Web service junkies, Ruby also has a mature SOAP library. If syndication is your thing, Ruby has a library that eases the handling of every major bizarre variant of RSS.

If you're a Macintosh programmer — as I am not — Ruby and most of its add-ons run very well on the new Unix-based OS. If you're used to Cocoa, a Ruby/Cocoa does a good job of integrating these two.

Windows programmers have libraries, such as Win32API and Win32OLE, that come with Ruby. Through other add-ons, you can interact with Windows apps via COM (arguably more easily than you can in VB); you can edit the registry, use the SAPI, and more. ActiveScriptRuby is a Windows Script Host (WSH) implementation; among other uses, it can be invoked inside Internet Explorer just like Javascript.

In short, when you program in Ruby, you're not limited to Ruby code. You can interface with other applications technologies, both local and remote, to leverage legacy apps and new tools alike. You won't outgrow it.

If you didn't like Ruby already, this article didn't convince you. The only way to evaluate a programming language is to use it. I encourage you to try it and see whether it meets your needs. If and when you need help, I recommend you talk to the people on the comp.lang.ruby newsgroup, a knowledgeable and friendly community. Welcome to Ruby!

Read more about scripting languages! Start with The State of the Scripting Universe. Then, explore each language in more detail, by learning Five Things You Didn't Know You Could Do...

  • ...With Python
  • ...With PHP
  • ...With Tcl
  • ...With Perl
  • Discuss this article in the DevSource forum.



     
     
    >>> More Languages Articles          >>> More By Hal Fulton
     



    DevSource video
    Devsource Video Series
    Manipulating Society through Technology
    Jeremy Bailenson, Director of the Virtual Human Interaction Lab at Stanford University, talks about virtual reality, avatars, Moore's law, how real world behaviors influence online reality, and societal manipulation through technology!
    >> Play video
    >> Read article
    >> See all videos
    DevLife Blog

    Julia explores the Robotics Studio! (It's for more than you think.)

    MSDev Blog

    Messages for Bill Gates!

    Make it Work
    .NET makes runtime type checking a breeze. See what Peter has to say about it in this week's tips!
    News
    Microsoft Counts on App Support for Vista
    Microsoft has taken pains to demonstrate that Windows Vista will have ample application support.
    DevSource RSS FEEDS
    XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.