From: Robert Klemme Date: 2011-01-29T00:43:53+09:00 Subject: Re: Each Array Method Skipping first Array position On Fri, Jan 28, 2011 at 4:26 PM, Chris R. wrote: > Could someone tell me why in the following code, when it iterates on the > array 'arraya' it skips the first position? Does it? > It is strange because > immediatley before each is called on arraya, I call inspect and you can > clearly see the first value is 1800, but the first value out put is > 1801. > > #code start > > startyear = nil > endyear = nil These assignments are superfluous. > arraya = [] > currentyearis = false > puts 'What year should I start with?' > startyear = gets.chomp > puts 'What year should I end with?' > endyear = gets.chomp > > > #assign years including and between start/endyear into array > > arraya = (startyear...endyear).to_a You do not do any checking on the input so you might get a sequence that you do not expect. > #iterate on array > > puts arraya.inspect > > arraya.each do |currentyear| >  if currentyear.to_i % 4 == 0 >    if currentyear.to_i % 100 == 0 >      puts currentyear.inspect You probably think somthing is omitted because the line above is guarded by a condition. You probably rather want to move this line up directly behind #each. >      puts('The Year ' + currentyear + ' is not a leap year.') if > currentyear.to_i % 400 == 0 >    else >      puts('The Year ' + currentyear + ' is a leap year.') >    end >  else >    puts('The Year ' + currentyear + ' is not a leap year') >  end > end > > #code end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/