[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

assignment operator that treats empty and nil as the same thing

scooterm@hotmail.com

8/29/2008 10:01:00 PM

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

5 Answers

matt

8/29/2008 11:51:00 PM

0

scooterm@hotmail.com <scooterm@hotmail.com> wrote:

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

Would it suffice to subvert Hash in such a way that the value of an
element whose value is the empty string is reported as nil?

class Hash
alias_method "origfetch", "[]"
def [](what)
result = origfetch(what)
if result == ''
nil
else
result
end
end
end

h = {"hey"=>"", "ho"=>"ha"}
h["hey"] ||= "homer"
p h

If that's too strong, you can devise your own function:

class Hash
def set_if_empty(k,v)
cur = self[k]
if cur.nil? || cur == ''
self[k] = v
end
end
end

h = {"hey"=>"", "ho"=>"ha"}
h.set_if_empty("hey", "homer")
p h



--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

RichardOnRails

8/30/2008 2:22:00 AM

0

On Aug 29, 6:01 pm, "scoot...@hotmail.com" <scoot...@hotmail.com>
wrote:
> ### 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.

The problem is that you think that otest['first_name] is nil in both
tests when you try the conditional assignment. As the following code
shows it's only nil the first time; the second time it's (an empty)
String.

otest = {};
puts otest['first_name'] .class.to_s
otest['first_name'] ||= 'homer';
puts otest.inspect;

### test2
otest = {'first_name'=>''};
puts otest['first_name'] .class.to_s
otest['first_name'] ||= 'homer';
puts otest.inspect;

HTH,
Richard

RichardOnRails

8/30/2008 2:33:00 AM

0

On Aug 29, 10:21 pm, RichardOnRails
<RichardDummyMailbox58...@uscomputergurus.com> wrote:
> On Aug 29, 6:01 pm, "scoot...@hotmail.com" <scoot...@hotmail.com>
> wrote:
>
>
>
> > ### 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.
>
> The problem is that you think that otest['first_name] is nil in both
> tests when you try the conditional assignment.  As the following code
> shows it's only nil the first time;  the second time it's (an empty)
> String.
>
> otest   =   {};
> puts otest['first_name'] .class.to_s
> otest['first_name'] ||= 'homer';
> puts otest.inspect;
>
> ### test2
> otest   =   {'first_name'=>''};
> puts otest['first_name'] .class.to_s
> otest['first_name'] ||= 'homer';
> puts otest.inspect;
>
> HTH,
> Richard

Woops. I only glanced at your post when I responded. I only
belatedly noticed that you understood the nil and and an empty string
were involved. My apologies.

RichardOnRails

8/30/2008 3:00:00 AM

0

On Aug 29, 10:21 pm, RichardOnRails
<RichardDummyMailbox58...@uscomputergurus.com> wrote:
> On Aug 29, 6:01 pm, "scoot...@hotmail.com" <scoot...@hotmail.com>
> wrote:
>
>
>
> > ### 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.
>
> The problem is that you think that otest['first_name] is nil in both
> tests when you try the conditional assignment.  As the following code
> shows it's only nil the first time;  the second time it's (an empty)
> String.
>
> otest   =   {};
> puts otest['first_name'] .class.to_s
> otest['first_name'] ||= 'homer';
> puts otest.inspect;
>
> ### test2
> otest   =   {'first_name'=>''};
> puts otest['first_name'] .class.to_s
> otest['first_name'] ||= 'homer';
> puts otest.inspect;
>
> HTH,
> Richard

Maybe I can redeem myself: Matt gave you a great solution in reopening
the Hash definition. I recommend a minor improvement as follows:

class Hash
alias_method "orig_fetch", "[]"
def [](what)
result = orig_fetch(what)
result = nil if result == ''
end
end

### test1
otest = {}
otest['first_name'] ||= 'homer'
puts otest.inspect #= {"first_name"=>"homer"}

### test2
otest = {'first_name'=>''}
otest['first_name'] ||= 'homer'
puts otest.inspect #= {"first_name"=>"homer"}

HTH,
Richard

Day Brown

2/21/2010 2:08:00 AM

0

Frito Pendejo wrote:
> It sounds like you are advocating social engineering, which I strongly
> disapprove of. You seem to believe that humans beings are domesticated
> animals like cattle, to be bred with traits of your choosing, and fed
> rations of nutritionally-balanced kibble to ensure optimum fattening
> before sending them to the slaughterhouse.
I am not advocating anything, but merely showing what smart women in
healthcare, who already know about all this, will choose to do. Its not
upta me, but them. They are the case managers, watching the case loads
increase, and feel the pressure to do something most acutely.