From: "Shot (Piotr Szotkowski)" Date: 2009-11-15T01:22:13+09:00 Subject: Re: Surprising behavior when extending instances --W/nzBZO5zC0uMSeA Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Jim Crossley: > This surprised me: > module English; def say; "yes"; end; end > module French; def say; "oui"; end; end > x =3D Object.new > x.extend English This adds English to the front of the method look-up path. > x.say # "yes" At this point, the #say method is first looked-up in English. > x.extend French This adds French to the front of the method look-up path. > x.say # "oui" At this point, the #say method is first looked-up in French. > x.extend English Hey, English is already in the method look-up path, no need to add it! > x.say # "oui" Oui, French is still at the front of the method look-up path=E2=80=A6 > What wrong assumptions am I making that this surprises me? That the second #extend call moved English to the front of the method look-up path. > What is the more elegant "Ruby way" of > alternating an object's behavior at runtime? I don=E2=80=99t have hand-on experience, and there probably is a simple, canonical way, but I=E2=80=99d start with looking at Object#instance_eval. =E2=80=94 Shot --=20 Anyone who finds the MS Office ribbon intuitive is not a member of the human species as I understand it. [William Morgan] --W/nzBZO5zC0uMSeA Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkr+2SsACgkQi/mCfdEo8UrZCQCghIAT2HHy26urDYLLvb1we0JV VCgAoKaLcKMdUAkl8HRTdVM7NlLTJn7S =2C2B -----END PGP SIGNATURE----- --W/nzBZO5zC0uMSeA--