From: Phrogz Date: 2009-12-24T12:20:17+09:00 Subject: Re: Date.gregorian_leap? code On Dec 23, 2:42 pm, Colin Bartlett wrote: > from date.rb >   # Is a year a leap year in the Gregorian calendar? >   # All years divisible by 4 are leap years in the Gregorian calendar, >   # except for years divisible by 100 and not by 400. >   def self.gregorian_leap? (y) y % 4 == 0 && y % 100 != 0 || y % 400 == 0 end > > Because of the precedence of && and || this is presumably parsed as >      ( y % 4 == 0 && y % 100 != 0 ) || y % 400 == 0 > and I'm trying to think of reasons why it is coded as it is instead of, say: >      y % 4 == 0 && ( y % 100 != 0 || y % 400 == 0 ) I suppose because either one works: irb> LEAP = [ 1796, 1804, 1896, 1904, 1992, 1996, 2000, 2004, 2008 ] irb> NOLEAP = [ 1799, 1800, 1801, 1802, 1803, 1805, 1899, 1900, 1901 ] irb> def test_leap irb> LEAP.each{ |y| puts "BAD! #{y} must leap" unless yield y } irb> NOLEAP.each{ |y| puts "BAD! #{y} musn't leap" if yield y } irb> end irb> test_leap{ |y| y % 4 == 0 && y % 100 != 0 || y % 400 == 0 } irb> test_leap{ |y| ( y % 4 == 0 && y % 100 != 0 ) || y % 400 == 0 } irb> test_leap{ |y| y % 4 == 0 && ( y % 100 != 0 || y % 400 == 0 ) }