Newbie Code question

Hello,

I reading the Agile Development Ruby book and came accross a line of
code that I dont quite understand…

errors.add(:price, “should be positive”) unless price.nil? || price >
0.0

The line of code works but I dont quite understand the logic behind it.
It prints the error message “price should be positive” when the price
entered is either 0 or less than 0.

This is where i dont understand the logic - the price.nil operator
returns a true if price is zero and the price > 0 returns a true if the
price is more than zero (logically this would have to be price < 0)?

If someone could explain the logic here then that would be great.

Thanks in advance!

On 10/2/06, DesertFox [email protected] wrote:

Hello,

I reading the Agile Development Ruby book and came accross a line of
code that I dont quite understand…

errors.add(:price, “should be positive”) unless price.nil? || price >
0.0

This is where i dont understand the logic - the price.nil operator
returns a true if price is zero

No. 0 is not nil.

irb(main):001:0> a = nil
=> nil
irb(main):002:0> a.nil?
=> true
irb(main):003:0> a = 0
=> 0
irb(main):004:0> a.nil?
=> false

Ugh, how I hate “unless”…

Unless is like “if not”. So…

errors.add() (price.nil? || price > 0.0)

So, when your price is <= 0.0…

errors.add() (false || false) # since price.nil? is false,
and price > 0.0 is false

errors.add() (false) # since (false || false) is false

so errors are added.

On 10/3/06, DesertFox [email protected] wrote:

returns a true if price is zero and the price > 0 returns a true if the
price is more than zero (logically this would have to be price < 0)?

price.nil? should not return true if the value of price is 0. nil is an
object that is the anti-matter of objects. It is something that isn’t
really there. It’s not 0, it’s not a blank string, they would be
something. No, this is nothing. ( does anyone have a racing snail? )
The
nil? method, available on all objects, will only return true if the
return
value is nil ( ie nothing )

So the logic of the code says

add an error to the price field unless price is nil (nothing) or if
price is
greater than 0.

This means that price can be nil (or blank or however you want to think
about it but blank is another method in rails) or it can be positive.
Any
other value and the error gets added to the price attribute.

Hope that helps a bit.

Michael C. wrote:

Ugh, how I hate “unless”…

Unless is like “if not”. So…

errors.add() (price.nil? || price > 0.0)

so errors are added.

Thanks for the replies guys. So, what would be a more sensible way to
write that line?

Thanks in advance.

DesertFox wrote:

Thanks for the replies guys. So, what would be a more sensible way to
write that line?

Thanks in advance.

I would say something like:

errors.add(:price, “should be positive”) if !price.nil? && price <= 0.0

Or, to be a bit more verbose:

if !price.nil?
if price <= 0.0
errors.add(:price, “should be positive”)
end
end

I guess that’s not a huge improvement. Basically, one way or another,
you have to check whether price is nil first, and then, if it isn’t,
check whether it’s less than or equal to 0, and that’s what makes the
line a bit complicated. Hope this helps!

if !price.nil?
if price <= 0.0
errors.add(:price, “should be positive”)
end
end

I guess that’s not a huge improvement. Basically, one way or another,
you have to check whether price is nil first, and then, if it isn’t,
check whether it’s less than or equal to 0, and that’s what makes the
line a bit complicated. Hope this helps!

Why not…

errors.add(:price, “should be positive”) if price.to_f < 0

This should work since…

nil.to_f
=> 0.0

Or,

errors.add(:price, “should be positive”) if !(price > 0)

-Nathan

Thanks guys. That helped. Now i can continue with the book.

Oops, sorry. Just realised nil is supposed to be a valid answer.

errors.add(:price, “should be positive”) if !(price > 0 || price.nil?)

-Nathan

So, what does this .nil statement do exactly? I think thats whats
causing the confusion for me.

Chris G. wrote:

DesertFox wrote:

Thanks for the replies guys. So, what would be a more sensible way to
write that line?

Thanks in advance.

I would say something like:

errors.add(:price, “should be positive”) if !price.nil? && price <= 0.0

Or, to be a bit more verbose:

if !price.nil?
if price <= 0.0
errors.add(:price, “should be positive”)
end
end

I guess that’s not a huge improvement. Basically, one way or another,
you have to check whether price is nil first, and then, if it isn’t,
check whether it’s less than or equal to 0, and that’s what makes the
line a bit complicated. Hope this helps!

coughJavacough

What makes the line above a bit complicated is not using the beauty and
power of ‘unless’ which exists entirely to stop you having to contort
boolean logic like this :slight_smile:

“Add the error unless the price is nil or positive”

versus

“If the price is not nil or if the price is less than or equal to zero,
add an error.”

I know which reads more easily for me :slight_smile:

A.

On 10/6/06, Al Cholic [email protected] wrote:

So, what does this .nil statement do exactly? I think thats whats
causing the confusion for me.

It’s actuall a question posed to the object
something.nil?

The method includes the ?

It says to the object “Hey, are you nil?”