From: Peter Hickman Date: 2013-05-31T19:06:47+09:00 Subject: Re: Eloquent Ruby Kaprekar's Number --001a11c38e2ccca05b04de00ab30 Content-Type: text/plain; charset=ISO-8859-1 For me 2 and 3 are unreadable, not to mention overly complicated. I would go with a modified version of 1 def kaprekar?(k) ks = (k**2).to_s lenks = ks.length lenf = (lenks)-1 lenh = (lenks/2)-1 a = ks[0..lenh].to_i b = ks[lenh+1..lenf].to_i k == (a+b) end You never use ks as it is you always use ks.to_s so we might was well go with that and make is easier to read. The line breaks make it more obvious as to the steps you are taking. Again it is just a personal preference. Finally the simple_test code does not work for failures. --001a11c38e2ccca05b04de00ab30 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
For me 2 and 3 are unreadable, not to mention overly compl= icated. I would go with a modified version of 1

def kaprekar?(k)
=A0 ks =3D (k**2).to_s
=A0 lenks =3D ks.length
<= div>
=A0 lenf =3D (lenks)-1
=A0 lenh =3D (lenks/= 2)-1

=A0 a =3D ks[0..lenh].to_i
=A0 b =3D ks[lenh+1..lenf].to_i

=A0 k =3D=3D (a+b)
end

You never use ks as it is you always use ks= .to_s so we might was well go with that and make is easier to read.
The line breaks make it more obvious as to the steps you are taking.

Again it is just a personal preference.

<= /div>
Finally the simple_test code does not work for failures.

--001a11c38e2ccca05b04de00ab30--