Experimenting with JRuby in Coldfusion

This is just a quick post, will post a longer one later when I’ve had more time to play.

Before this will work, you’ll need to download and install JRuby, and copy jruby.jar from the installation directory to your Coldfusion classpath. For me those directories were c:\program files (x86)\jruby-1.4.0\lib and c:\Coldfusion9\lib respectively.

Once that’s done, on to the fun!

First, I created a really simple custom tag called ruby.cfm as follows:

<cfscript>
ruby = createObject("java", "org.jruby.embed.ScriptingContainer");
if (thisTag.ExecutionMode is "end") {
  thisTag.GeneratedContent = ruby.runScriptlet(toString(thisTag.GeneratedContent));
}
</cfscript>

Then, I created my test page, test_ruby.cfm:

<cfset name = "Bob" />
<cfoutput>
  <cf_ruby>
    def myproc(str)
      "Hello ##{str}!"
    end
    myproc("#name#")
  </cf_ruby>
</cfoutput>

When you load up the test page in your browser, you should see:

Hello Bob!

WARNING: Running this code may cause a rip in the space-time continuum.

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Technorati
  • Twitter
This entry was posted in Coldfusion. Bookmark the permalink.

2 Responses to Experimenting with JRuby in Coldfusion

  1. Don Blaire says:

    My boss was inquiring of me if one can use ruby with coldfusion. This came just at the right time. Thanks.

  2. Mike Hodgson says:

    Glad I could help Don. I haven’t really done any in-depth experimenting with this, so there is probably a better way to do it. Please let me know how you make out!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">