Rexml help

Hi I am creating a hash as detailed below

hPhotoDetails = {
:photoId =>
pDoc.elements[’/rsp/photo’].attributes.get_attribute(“id”).value,
:title => pDoc.elements[’/rsp/photo/title’].text,
:description => pDoc.elements[’/rsp/photo/descripton’].text,
}

if the element /rsp/photo/descripton == or does not exist
an error is thrown.

Is there a way to get it to add nil if it does not exist without
breaking out of the hash creation?

Thanks
jon

jon wrote:

:description => pDoc.elements[‘/rsp/photo/descripton’].text,

:description => de_nil(pDoc.elements[‘/rsp/photo/descripton’]).text,

def de_nil(q)
return q if q
require ‘ostruct’
return OpenStruct(:text => ‘’)
end

That’s the “Null Object Refactor” in a nutshell. Google for all my
terms because I might have the details wrong.


Phlip
http://c2.com/cgi/wiki?ZeekLand ← NOT a blog!!