small medium large xlarge

14 Jun 2010, 14:49
Abder-Rahman Hasan Ali (5 posts)

What is meant by this paragraph in page (88)?

Note that before we compare the price to 0.01, we first check to see whether it’s nil. This is important: if the user leaves the price field blank, no price will be passed from the browser to our application, and the price variable won’t be set. If we tried to compare this nil value with a number, we’d get an error.

17 Jun 2010, 17:57
Sam Ruby (634 posts)

This paragraph is describing the following lines of code:

errors.add(:price, 'should be at least 0.01' ) if price.nil? ||
   price < 0.01

Note the if price.nil? prior to the check for price < 0.01. This is present in order to prevent the error that would occur if the user entered no input. If the user provides no input, price would be nil, and the comparison of nil to 0.01 would fail.

You must be logged in to comment