From: dblack@... Date: 2007-09-03T20:12:54+09:00 Subject: Re: Ruby stupidities --1926193751-1923253114-1188817964=:19797 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-1923253114-1188817964=:19797" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --1926193751-1923253114-1188817964=:19797 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Mon, 3 Sep 2007, Matthias W=C3=A4chter wrote: > On 31.08.2007 05:24, Kenneth McDonald wrote: >> Having said that, here's my first entry. I think it's utterly stupid >> (can you tell I just wasted some time tracking this down?) that >> >> "abc"[0] =3D=3D "a" > > You can change that behavior yourself without installing >=3D1.9 > > class String > alias :old_index :[] > def [](*a) > a.length=3D=3D1 ? old_index(*a).chr : old_index(*a) > end > def asc # this is from [1] > case size > when 0; nil > when 1; old_index(0) > else unpack 'c*' > end > end > end > > "abc"[0] =3D=3D "a" # -> true > "abc"[1] =3D=3D "b" # -> true > "abc"[0,1] =3D=3D "a" # -> true > "a".asc =3D=3D 97 # -> true > "abc".asc =3D=3D [97,98,99] # -> true > > But certainly this may break existing libs, so be careful. The problem is, you can't be careful. You can only decide that it's OK for code to break, and keep your fingers crossed that it doesn't. I don't mean to split hairs -- of course "careful" can mean lots of things. It's just that I think it's important to remember that the code doesn't "know" that the person who changed it is worried :-) It's just going to do what it does. So unless you're willing to read through the entire source code base, for Ruby and all add-on libraries, every time you make such a change, you're pretty much accepting some very real risk. David --=20 * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com) --1926193751-1923253114-1188817964=:19797-- --1926193751-1923253114-1188817964=:19797--