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