On Fri, Jan 28, 2011 at 4:26 PM, Chris R. <chris.gogreen / gmail.com> 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 =3D nil
> endyear =3D nil

These assignments are superfluous.

> arraya =3D []
> currentyearis =3D false
> puts 'What year should I start with?'
> startyear =3D gets.chomp
> puts 'What year should I end with?'
> endyear =3D gets.chomp
>
>
> #assign years including and between start/endyear into array
>
> arraya =3D (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|
> =A0if currentyear.to_i % 4 =3D=3D 0
> =A0 =A0if currentyear.to_i % 100 =3D=3D 0
> =A0 =A0 =A0puts 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.

> =A0 =A0 =A0puts('The Year ' + currentyear + ' is not a leap year.') if
> currentyear.to_i % 400 =3D=3D 0
> =A0 =A0else
> =A0 =A0 =A0puts('The Year ' + currentyear + ' is a leap year.')
> =A0 =A0end
> =A0else
> =A0 =A0puts('The Year ' + currentyear + ' is not a leap year')
> =A0end
> end
>
> #code end

Kind regards

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/