An Architect's View

CFML, Clojure, Software Design, Frameworks and more...

An Architect's View

Ruby example in ColdFusion 8

June 2, 2007 ·

Here's a couple of examples of Ruby running inside ColdFusion 8. First off, the example that follows the lead of the PHP example (albeit a little more complicated):
<cfset session.marker = "Hi, I'm a session variable!" />
<cfset who = "Sean" />
<cftimer label="Executing Ruby" type="outline">
      # since we need to return the last expression in the script
      # we run other code first, unless the PHP example:
      # set a page level variable in ColdFusion:
      $coldfusion["greeting"] = "wibble"
      # read a URL variable and set a session variable in ColdFusion:
      $test = $url["test"]
      if $test then
         $session["what"] = $url["test"]
         $session["what"] = "test was not passed as a URL variable"
      # this builds a single string which is the result of the script:
      "Hi " + $coldfusion["who"] + "<br />" +
      $session["marker"] + "<br />" +
      ($test ? $test : "") + "<br />" +
      $cgi["SCRIPT_FILENAME"] + "<br />"
Several ColdFusion scopes are available directly in Ruby as global variables (the variables scope as $coldfusion and CGI, form, session and URL as global variables of the same name, lowercased). Unlike the PHP scripting engine, any actual output from Ruby is lost (puts, print) or, more accurately, written directly to the system console which is not very helpful. Instead, the last expression evaluated is converted to a string and used as output of the custom tag. Here's another example showing a Ruby class used with a block and an iterator:
<cftimer label="Executing Ruby with a class" type="outline">
      class Person
         def initialize(firstName,lastName)
            @firstName = firstName
            @lastName = lastName
         attr_reader :firstName, :lastName
      people = ["Ben","Forta"),"Jason","Delmore"),"Tim","Buntel")
      people.collect {
         person.firstName + " " + person.lastName + " rocks!<br />"
I haven't done much testing with the Ruby version yet, in particular what happens when you manipulate Ruby objects in ColdFusion (or vice versa). Python, Groovy and Haskell are on my list. I may go through the entire Sun Scripting Engine list eventually (Awk, Tcl and a variety of strange pseudo-languages are there).

Tags: coldfusion · oss

3 responses

  • 1 sal // Jun 3, 2007 at 10:37 AM

    damn! that just crazy... Ruby and CF on the same page. wow! thats right up my alley. Not sure I'll integrate them, but good to know its a possibility.
  • 2 Mike Henke // Sep 20, 2010 at 7:39 AM

    Sean, I tried the examples on cf8 but the page outputted the code between <invalidTag:ruby>. I must be missing something obvious :-) Could you point me in the correct direct please? Thanks,
  • 3 Sean Corfield // Sep 21, 2010 at 9:27 AM

    @Mike, sounds like you don't have a cfimport tag to bring in the library (with the prefix="script"). Without that, CF8 converts the script:ruby tag to invalidTag:ruby because of it's XSS logic.

    See the code examples in the earlier blog postings about my scripting project (and also the docs / examples that go with it).

    FWIW, this project is essentially dead and I recommend folks look at Barney Boisvert's excellent cf_groovy project instead.