Funny how similar problems occur in different places at the same time. I
just fixed the exact same issue in my code (not related to REXML however),
with an ugly if construct. But how about this for a fix.

class NilClass
	def ifnil(obj)
		obj
	end
end

class Object
	def ifnil(obj)
		self
	end
end

Here we can test it.

def f
  return nil
end

def g
  return "abcde"
end

p f().ifnil("qrstu").chop  --> qrst
p g().ifnil("qrstu").chop  --> abcd

Then you could write

@company = customer.elements["company"].text.ifnil("").chomp

Steve Tuckner

> -----Original Message-----
> From: Andreas Schwarz [mailto:usenet / andreas-s.net]
> Sent: Friday, May 02, 2003 2:08 PM
> To: ruby-talk ML
> Subject: chomp'ing REXML:Element.text
>
>
> Hello,
>
> I'm using the following code to read and chomp the content of a few
> elements from REXML. The problem is that I get an exception when an
> element is empty (<element></element>) because .text returns nil then.
> How can I solve this problem without writing large if-constructions?
>
> @company = customer.elements["company"].text.chomp
> @salutation =customer.elements["salutation"].text.chomp
> [...]
>
> Thanks
> Andreas
>
> --
> AVR-Tutorial, ?ber 350 Links
> Forum f?r AVRGCC und MSPGCC
> -> http://www.mikrocontroller.net