"unless" keyword

"Unless" == "if not"

so

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

Is equivalent to

errors.add(:price, "should be positive") if !price.nil? and price > 0.0

···

-----Original Message-----
From: Mike Novecento [mailto:mike.novecento@gmail.com]
Sent: Friday, 16 September 2005 1:42 PM
To: ruby-talk ML
Subject: "unless" keyword

Ok guys, you convinced me: Ruby is the way to go. I have been reading
online for a few hours now, and it looks simply fantastic (especially
with Ruby on Rails).
I am so excited, I have been programming for years, and never thought it
could be so fun.

I have a first question. Looking at some code online, I have seen this
line of code from a the site of pragmatic programmer:

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

I understand that it is required to validate that a price is strictly
positive.
My question, maybe very easy, is... how does it exactly work?

I thought it should be "ADD ERROR unless NOT NIL AND PRICE > 0.0"...
why he uses (OR) ||? Maybe I am missing the logic of the keyword
"unless". I feel dumb :slight_smile:

Thanks in advance,
Mike.

Neville Burnell wrote:

"Unless" == "if not"

so

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

Is equivalent to

errors.add(:price, "should be positive") if !price.nil? and price > 0.0

I think you mean
errors.add(:price, "should be positive") if not (price.nil? and price >
0.0)
-or-
errors.add(:price, "should be positive") if !price.nil? and price <=
0.0

-Charlie

···

-----Original Message-----
From: Mike Novecento [mailto:mike.novecento@gmail.com]
Sent: Friday, 16 September 2005 1:42 PM
To: ruby-talk ML
Subject: "unless" keyword

Ok guys, you convinced me: Ruby is the way to go. I have been reading
online for a few hours now, and it looks simply fantastic (especially
with Ruby on Rails).
I am so excited, I have been programming for years, and never thought it
could be so fun.

I have a first question. Looking at some code online, I have seen this
line of code from a the site of pragmatic programmer:

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

I understand that it is required to validate that a price is strictly
positive.
My question, maybe very easy, is... how does it exactly work?

I thought it should be "ADD ERROR unless NOT NIL AND PRICE > 0.0"...
why he uses (OR) ||? Maybe I am missing the logic of the keyword
"unless". I feel dumb :slight_smile:

Thanks in advance,
Mike.