Hash.from_xml

This is just an odd little note, since it took me a couple of searches to determine what the deal was. I had an XML string that I got from a SOAP call, and I wanted to do a quick conversion to JSON. Since I know the string’s going to be fairly small, the overhead’s not going to be too bad. So I tried to do a simple:

puts Hash.from_xml(response).to_json

Oddly, I got undefined method `from_xml' for Hash:Class (NoMethodError). That’s weird, I thought; I know that method exists. After checking ruby-docs, though, in fact it doesn’t — if you’re not in Rails. I was running this as a standalone test program. Sure enough, I brought up a Rails console and tested it, and there’s the method.

It just took a quick check then to confirm that, indeed, the from_xml() method is added by ActiveSupport. Putting this in:

require 'active_support'

Took care of the problem. Although I also had to add a to_s to the response in order to make it look like a String.

UPDATE: Thanks to Andrew for his comment below. Now for ActiveSupport 3 you may need to use this instead:

require ‘active_support/core_ext’

Silly little problem, but in case this will help someone out, here it is.

Advertisements
8 comments
  1. Andrew said:

    That didn’t work for me.

    This did:

    require ‘active_support/core_ext’

  2. Thanks for this (and thanks to Andrew for the active support 3.0.x version)

  3. Dean said:

    Thanks Andrew! They change things around and it could and would have caused me a major nightmare if I had not seen your post

  4. I suggest a post update, I you miss Andrew’s comment you’re screwed!

    Anyway thanks for the tip.

  5. This didn’t work for me. But it did when i required ‘active_support/core_ext/hash’
    Strange, but it works now…

  6. Jinesh said:

    Thanks Andrew…..This is working for Me.,,,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: