S
scooterm
### 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.
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.