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 () 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

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 () 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

In article slrnbb5ftt.2cv.usenet@home.andreas-s.net,

···

Andreas Schwarz usenet@andreas-s.net wrote:

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 () 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
[…]

You can use a rescue e.g.

@salutation =customer.elements[“salutation”].text.chomp rescue ‘’

but there may be better ways of doing it.

Hope this helps,

Mike

mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA

Hi –

···

On Sat, 3 May 2003, Andreas Schwarz wrote:

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 () 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

You could throw in a to_s:

…text.to_s.chomp

(since nil.to_s is “”)

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

dblack@superlink.net wrote:

···

On Sat, 3 May 2003, Andreas Schwarz wrote:

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 () 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

You could throw in a to_s:

…text.to_s.chomp

(since nil.to_s is “”)

I already thought about this, but I thought nil.to_s would return
"nil"… stupid… should have tried it before.

Thanks!

That doesn’t work, I’m afraid. Not in 1.6 anyway.

The ‘rescue’ modifier only rescues a particular kind of exception, and
"no method for that object" does not fall in that category.

Gavin

···

On Saturday, May 3, 2003, 5:48:14 AM, Mike wrote:

You can use a rescue e.g.

@salutation =customer.elements[“salutation”].text.chomp rescue ‘’

but there may be better ways of doing it.

I already thought about this, but I thought nil.to_s would return
"nil"… stupid… should have tried it before.

…or you could define:

class NilClass; def chomp; “”; end; end

-t