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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On 10/3/06, DesertFox <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > 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)?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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On 10/2/06, DesertFox <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 zeroNo. 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() <if not> (price.nil? || price > 0.0) So, when your price is <= 0.0... errors.add() <if not> (false || false) # since price.nil? is false, and price > 0.0 is false errors.add() <if not> (false) # since (false || false) is false so errors are added. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Michael Campbell wrote:> Ugh, how I hate "unless"... > > Unless is like "if not". So... > > errors.add() <if not> (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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
>> 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!Why not... errors.add(:price, "should be positive") if price.to_f < 0 This should work since...>> nil.to_f=> 0.0 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Thanks guys. That helped. Now i can continue with the book. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
njmacinnes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Oct-04 06:10 UTC
Re: Newbie Code question
Or, errors.add(:price, "should be positive") if !(price > 0) -Nathan On 03/10/06, Philip Hallstrom <rails-SUcgGwS4C16SUMMaM/qcSw@public.gmane.org> 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! > > Why not... > > errors.add(:price, "should be positive") if price.to_f < 0 > > This should work since... > > >> nil.to_f > => 0.0 > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
njmacinnes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Oct-04 06:14 UTC
Re: Newbie Code question
Oops, sorry. Just realised nil is supposed to be a valid answer. errors.add(:price, "should be positive") if !(price > 0 || price.nil?) -Nathan On 04/10/06, njmacinnes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <njmacinnes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Or, > > errors.add(:price, "should be positive") if !(price > 0) > > -Nathan > > > > On 03/10/06, Philip Hallstrom <rails-SUcgGwS4C16SUMMaM/qcSw@public.gmane.org> 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! > > > > Why not... > > > > errors.add(:price, "should be positive") if price.to_f < 0 > > > > This should work since... > > > > >> nil.to_f > > => 0.0 > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Chris Gernon 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!*cough*Java*cough* 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 :-) "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 :-) A. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
So, what does this .nil statement do exactly? I think thats whats causing the confusion for me. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On 10/6/06, Al Cholic <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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?" --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---