From: dblack@... Date: 2006-12-03T09:06:59+09:00 Subject: Re: loops and branching Hi -- On Sun, 3 Dec 2006, Pete Yandell wrote: > > On 02/12/2006, at 3:02 AM, Jason Mayer wrote: > >> puts 'Enter a starting year' >> num1 = gets.chomp.to_i >> puts 'Enter an ending year' >> num2 = gets.chomp.to_i >> puts 'The leap years between ' + num1.to_s + ' and ' + num2.to_s + ' are: >> ' >> (num1..num2).select do |x| >> if (x % 4 == 0) && (x % 100 != 0) || (x % 400 == 0) >> puts x >> end >> end > > Errr...that's not really a good use of select. You probably meant: > > (num1..num2).each do |x| > if (x % 4 == 0) && (x % 100 != 0) || (x % 400 == 0) > puts x > end > end > > Although I prefer separating the calculation and the output: > > leap_years = (num1..num2).select {|x| (x % 4 == 0) && (x % 100 != 0) || (x % > 400 == 0) } > leay_years.each do {|x| puts x } s/leay/leap/ :-) Also you can just do: puts leap_years which will call puts on each element in the array. David -- David A. Black | dblack@wobblini.net Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org