Forum: Ruby on Rails rexml help

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
4ee328064244984785b6fb357482ea0c?d=identicon&s=25 jon (Guest)
on 2006-12-27 21:43
Hi I am creating a hash as detailed below

hPhotoDetails = {
 :photoId =>
 :title => pDoc.elements['/rsp/photo/title'].text,
 :description => pDoc.elements['/rsp/photo/descripton'].text,

if the element /rsp/photo/descripton == <description/> 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?

Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2006-12-27 23:40
(Received via mailing list)
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 => '')

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

  Phlip  <-- NOT a blog!!
This topic is locked and can not be replied to.