Assignment operator that treats empty and nil as the same thing

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

scooterm@hotmail.com wrote:

I would like to handle this with a *single* assignment operator, just
like you can do in test1.

There is no such operator in Ruby. You have to make an if statement or
something. Also remember that if the value is nil, calling empty? on it
raises an error, so you have to check first for nil? and then for
empty?.

You can make it a bit easier with something like this:

class Object
  def nothing?
    if nil?
      true
    elsif respond_to? :empty? and empty?
      true
    elsif respond_to? :zero? and zero?
      true
    else
      false
    end
  end
end

You won't have to check both conditions now.

TPR.

···

--
Posted via http://www.ruby-forum.com/\.

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

···

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.

--
matt neuburg, phd = matt@tidbits.com, Matt Neuburg’s Home Page
Leopard - http://www.takecontrolbooks.com/leopard-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com

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

···

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.

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.

···

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