Small conditional statement not working

While I’m pulling data out of a form I’m trying to do this

unless v[:state_id] = 0 # Should be interpreted as the state_id is the number zero.

However it doesn’t seem to be working for some reason.

The conditional about it is unless v[:state_id].blank? # this conditional works unless v[:state_id] = 0

I’ve tried single quotes, double quotes and nothing seems to be stopping it.

Any obvious reasons ?

Stuart

hi stuart!

Dark Ambient [29.10.2006 22:05]:

While I'm pulling data out of a form I'm trying to do this

unless v[:state_id] = 0 # Should be interpreted as the state_id is the number zero.

what you're actually doing here is *assigning* (=) the value 0 to your variable (returning that value, which in ruby is true), while what you'd rather want is *testing* for equality (==).

long story short: s/=/==/ will do the trick :wink:

cheers jens

hi stuart!

Dark Ambient [29.10.2006 22:05]:

While I’m pulling data out of a form I’m trying to do this

unless v[:state_id] = 0 # Should be interpreted as the state_id is the number zero.

what you’re actually doing here is assigning (=) the value 0 to your variable (returning that value, which in ruby is true), while what you’d rather want is testing for equality (==).

Well I did try == 0 , and basically layed there like a dead mouse.

long story short: s/=/==/ will do the trick :wink:

what is this ? is this a typo , because this won’t parse .

Stuart

Dark Ambient [29.10.2006 22:19]:

long story short: s/=/==/ will do the trick :wink:

what is this ? is this a typo , because this won't parse .

well, it just means that you have to substitute the single = by ==.

however, you said that your data came out of a form which probably makes them strings, so you might try:

  v[:state_id] == '0'

or

  v[:state_id].to_i == 0

hth jens

Well, I give up , cause none of these plus all the one’s I’ve tried don’t seem to make a difference.

Stuart

unless v[:state_id] == 0 will return true only if v[:state_id] does not equal zero, is that what you are wanting to do? Or do you want it to check if state_id is not set? (unless v[:state_id])

Dark Ambient wrote:

unless v[:state_id] == 0 will return true only if v[:state_id] does not equal zero, is that what you are wanting to do? Or do you want it to check if state_id is not set? (unless v[:state_id])

If the state_id IS 0 (meaning the number zero). So I don’t think it should be == , I think it should be = , but I did a != 0 and that really mucked things up.

Stuart

Exactly what are you trying to do?

do something 'unless v[:state_id] is zero' do something 'unless "setting v[:state_id] to zero evalutates to true"' do something 'unless v[:state_id] is not zero'

Exactly what are you trying to do?

do something ‘unless v[:state_id] is zero’ <-------------- Yes this one.

Stuart

Dark Ambient wrote:

> Exactly what are you trying to do? > do something 'unless v[:state_id] is zero' <-------------- Yes this one.

do_something unless v[:state_id] == 0

or

unless v[:state_id] == 0   do_something end

I suspect that your values are not what you think they are, or that you are having trouble properly testing what is going on. If you are testing for equality, you definitely, absolutely, always want a double equals sign. That you were confused about that makes me think that your life might become easier if you read a beginner's book on programming in Ruby.

I’m not sure what is meant by equality but if I remember correctly from reading a beginner’s book, equality is not the same as comparing one value to the next. All I am trying to ask (Ruby) is the value in v[:state_id] zero. In that case I believe one set of equal signs is correct.

Let me know if I’m wrong or if there is some misunderstanding here.

Stuart