assignment operator that treats empty and nil as the same thing
- From: "scooterm@xxxxxxxxxxx" <scooterm@xxxxxxxxxxx>
- Date: Fri, 29 Aug 2008 15:01:03 -0700 (PDT)
### test1
otest = {};
otest['first_name'] ||= 'homer';
puts otest.inspect;
### test2
otest = {'first_name'=>''};
otest['first_name'] ||= 'homer';
puts otest.inspect;
Problem: test1 produces the correct output, but test2 produces
unwanted output (the value remains as an empty string). I realize that
ruby treats the empty string as "true" ... nevertheless I want to find
an assignment operator in ruby that treats nil and empty exactly the
same way, with the behavior shown in test1.
Question: Is there a way to do this in ruby *without* resorting to an
if statement or some other construct such as:
otest['first_name'] = (otest['first_name'].empty?) 'homer' :
otest['first_name'];
I would like to handle this with a *single* assignment operator, just
like you can do in test1.
.
- Follow-Ups:
- Re: assignment operator that treats empty and nil as the same thing
- From: RichardOnRails
- Re: assignment operator that treats empty and nil as the same thing
- From: matt neuburg
- Re: assignment operator that treats empty and nil as the same thing
- Prev by Date: Re: Does Ruby optimize tail-call recursion?
- Next by Date: Re: assignment operator that treats empty and nil as the same thing
- Previous by thread: Re: Ncurses module inclusion question
- Next by thread: Re: assignment operator that treats empty and nil as the same thing
- Index(es):
Relevant Pages
|