Monday, July 13, 2009

Easy XML Response Strings in Ruby with Multi-line String

Using a multi-line String in Ruby is sometimes a good way of generating an XML response string. Thanks to Jim for the pointer to this operator:
        def self.get_some_xml(hash_in)
          resp = <<-eos<?xml version='1.0'?>'  
<myresponse version='1.0'> 
  <list> 
    <item> 
      <name>ACME</name> 
      <value>#{hash_in['some_key']}</value> 
      ...
    </item>
    ...
  </list>
  ...
</myresponse>
          eol
          
          resp
        end
In the end though, I just used doublequote to start and end and changed all quotes to single quotes in the message.

More info at: http://blog.jayfields.com/2006/12/ruby-multiline-strings-here-doc-or.html

No comments: